Line Cheng in C #

Source: Internet
Author: User

One, the reason for using threads

1. You can use threads to isolate code from other code to improve application reliability.

2. You can use threads to simplify coding.

3. Threads can be used to implement concurrent execution

II. Basic Knowledge

1, process and thread: The process acts as the basic unit of the Operating system execution program, owns the resources of the application, the process contains the threads, the resources of the process are shared by the threads, and the threads do not own the resources.

2. Foreground thread and background thread: New line Cheng is considered foreground thread through the thread class. When all foreground threads are closed, all background threads are also terminated directly, without throwing an exception.

3, Suspend (Suspend) and Wake (Resume): Because the execution order of the thread and the execution of the program is unpredictable, so use suspend and wake prone to deadlock situations, in the actual application should be used sparingly.

4. Blocking thread: Join, blocks the calling thread until the thread terminates.

5. Terminating thread: Abort: Throws the ThreadAbortException exception to terminate the thread, and the terminated thread cannot wake. Interrupt: Throws the threadinterruptexception exception to let the thread terminate, can continue execution by catching the exception.

6, Thread priority: AboveNormal BelowNormal highest Lowest normal, default to normal.

Third, the use of threads

Thread functions can be passed through a delegate, without parameters, or with parameters (only one parameter), and can encapsulate parameters with a class or struct body.

You can also write as follows:

Thread Selectdatathread = selectdatathread = new Thread (Time_click);
 selectdatathread.isbackground = true;                         
  Isstartthreadonetimes[providers_code] = false;//sets a lock for each providercode so that the thread is executed only once
 selectdatathread.start ();

void Time_click (Object target)
{
System.Windows.Forms.MessageBox.Show ("Time_click (object target)");
Thread.Sleep (10000);//delay 10s thread execution
Timerforreadjsondatabyprovidercode ();
Thread.Sleep (15000);//Delay 15s re-loop execution
Time_click (target);
}

Iv. thread Pool

Because of the cost of creating and destroying threads, excessive use of threads can cause memory resources to be wasted, and the concept of a thread pool is introduced for performance reasons. The thread pool maintains a request queue, the thread pool's code extracts the task from the queue, and then it is delegated to a thread pool for execution, and the threads are not destroyed immediately after execution, so that tasks can be performed in the background and the overhead of thread creation and destruction can be reduced.

Thread pool thread Cheng is considered a background thread (IsBackground).

Namespace Test {

Class Program {

static void Main (string[] args) {//Adds a work item to the thread pool queue, where a thread parameter can be passed

ThreadPool.QueueUserWorkItem (TestMethod, "Hello");

Console.readkey (); }

public static void TestMethod (object data) {

String datastr = data As String;

Console.WriteLine (DATASTR);

}

}

}

V. Task class

Using ThreadPool's QueueUserWorkItem () method to initiate an asynchronous thread execution is simple, but the biggest problem with this method is that there is no built-in mechanism to let you know when the operation is complete, and there is no built-in mechanism to get a return value after the operation is complete. To do this, you can use the task class in System.Threading.Tasks.

Constructs a Task<tresult> object and passes the return type of an operation for the generic TResult parameter

Line Cheng in C #

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.