General summary and summary of Multithreading

Source: Internet
Author: User

1. Why multithreading?

(1) Let the computer "do a lot of things at the same time" to save time.

(2) running the program in the background improves the running efficiency of the program and does not cause no response on the main interface.

(3) multithreading allows a program to "simultaneously" handle multiple tasks.

(4) The computer CPU is idle most of the time, wasting CPU resources.

  1. Process and thread (illustration)

 

(1) A process must have at least one thread.

(2) "concurrent execution" can be performed between multiple threads in the same process ".

(3) A thread is an execution stream in the program. Each thread has its own proprietary register (Stack pointer, program counter, etc.), but the code zone is shared, that is, different threads can execute the same function. Multithreading means that a program contains multiple execution streams, that is, a program can run multiple different threads to execute different tasks (CODE) at the same time ), that is to say, a single program is allowed to create multiple parallel threads to complete their respective tasks.

 

  1. How to Implement multithreading in. NET

(1) The thread must also execute a piece of code. To generate a thread, you must first write a method for the thread. The code in this method is the code to be executed by the thread. (Find someone to do things ).

(2) The method is called by Delegate when the thread starts. (When the thread starts, the delegate passed by the call will execute the corresponding method to implement the thread execution method ).

  1. Four steps to generate a thread

(1) Compile the method to be executed by the thread

(2) reference the System. Threading namespace

(3) instantiate the Threading class and pass in a delegate pointing to the method to be run by the thread (this thread has been generated but has not yet run ).

(4) Call the start method of the Thread instance to mark that the Thread can be executed by the CPU, but the specific execution time is determined by the CPU.

Example: demonstrates some features of multithreading. The implementation code is described below:

 

(1) The Code implemented under the fault event of a single thread is to first define a method to achieve a 99999 cyclical increase and call this method under the event of the fault button of a single thread, the Code is as follows:

Private void CountTime ()

{

DateTime beginTime = DateTime. Now;

For (int I = 0; I <999999999; I ++)

{

}

TimeSpan ts = beginTime. Subtract (DateTime. Now );

MessageBox. Show ("cyclic completion" + ts. Milliseconds );

}

The execution process of this Code is: when the user clicks the button, the event will be triggered, and then the CPU will process the event, at this time, no matter how we drag the form or click somewhere, there will be no response until the event is executed. How can we solve this problem? We use multithreading.

(2) When the code below the event of the multi-threaded resolution button is:

// Use multiple threads to solve the problem of UI freezing

Private void btnMulThread_Click (object sender, EventArgs e)

{

// Create a thread object and pass in the UI to the thread

Thread threadFirst = new Thread (CountTime );

// Set the thread to a background thread

ThreadFirst. IsBackground = true;

// Start the thread and run the command

ThreadFirst. Start ();

}

The execution process of this code solves the problem we mentioned above. This time, when we click this control event, we can do anything, because it is a multi-threaded program.

  1. Foreground thread and background thread

(1) foreground program: the program can be closed only when all foreground threads are closed.

(2) Background Program: the background thread ends automatically only when all foreground threads end.

  1. Important member of Thread class (*)

(1) Start () Start thread

(2) Abort () terminate a thread

(3) Thread. Sleep (1) static method. It can be that the current Thread stops running for a period of time.

(4) Name thread Name

(4) Thread. CurrentThread obtains the current Thread reference

  1. Multithreading with Parameters

(1) As shown in: There is a multi-threaded execution button with parameters in the figure. The Code implemented under the event of this button is: Define a method first, code used to obtain information entered by a user:

Private void ShowTxtName (object name)

{

If (name. ToString ()! = "")

{

MessageBox. Show ("name =" + name. ToString ());

}

Else

{

MessageBox. Show ("null ");

}

}

The code for the method called below the control event is:

Private void btnThreadWithPara_Click (object sender, EventArgs e)

{

Thread thread = new Thread (ShowTxtName );

Thread. IsBackground = true;

Thread. Start (txtName. Text );

}

  1. Multi-threaded implementation with multiple parameters

(1) The program code for implementing a method with multiple parameters in multiple threads is: Define an array or set to implement this method. If there is a button for implementing a method with multiple parameters in multiple threads, the code below the event of this button is:

First, define a method and use an array or set to obtain the input name: the code is as follows:

Private void ShowTxtManyName (object li)

{

List <string> list = li as List <string>;

If (list! = Null)

{

Foreach (string s in list)

{

MessageBox. Show (s );

}

}

}

The code for implementing the method below the event of this button is:

Private void btnThreadManyPara_Click (object sender, EventArgs e)

{

Thread thread = new Thread (ShowTxtManyName );

Thread. IsBackground = true;

Thread. Start (new List <string> () {"Jackie Chan", "Han Yinglong", "bruce lee "});

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.