C # comparison between threads and thread pools in High-positioning and high-efficiency programs

Source: Internet
Author: User

ApplicationProgramThere can be multiple threads. When the program needs a lot of time to process some businesses or processes, it can be operated through threads. If the application does not use a thread, this will cause a lot of trouble. For example, there is a datagridview on the interface that needs to fill in data. If there is a form_load event to bind data, if you operate in the main thread, when you open the form, the program will be suspended. this is not accepted by the customer, so in program development, the thread application is an indispensable part.

We use multithreading to develop programs. The purpose is to reduce the idle time of the processor unit and increase the operation capability of the processor unit. however, this does not mean that multithreading can be used in the program. Improper application may reduce the system performance. for example, if the time required to complete a job is N, in the case of multithreading, the time required to create a thread is N0, and the time required to process the job in the thread is N1, the time required for thread destruction is N3, and some other time is not counted here. It can be seen that if the job process is simple, when resources are used and time is not long, you do not need to use a thread for processing. If a job needs to constantly create a thread, you need to use it in the thread pool to improve program performance.

The system. Threading namespace needs to be added for multi-threaded jobs. The following describes a thread operation:

Use a thread to create and delete a thread at a time.
Creation thread:
Thread th = new thread (New threadstart (method ));
Start thread:
Th. Start ();
Destroy thread:
Th. Abort ();
Thread Name:
Th. Name = "";
Thread sleep:
Th. Sleep ();
Thread suspension:
Th. Suspend ();
Thread recovery:
Th. Resume ();
Another way to talk about thread is to set the background thread.
If the isbackground attribute is set to true, this thread is the background thread.
There is only one difference between the background thread and the foreground thread, that is, the background thread does not prevent program termination. Once all foreground threads of a process are terminated,
CLR will call the abort () method of any surviving background process to completely terminate the process. Note: The background thread cannot directly operate the process
Data references.

 

Use threadpool to create threads. If your program needs to frequently create threads, you can use this class to create threads and improve the process.
Sequential performance. Use threadpool. queueuserworkitem (New waitcallback (method), 1) to create a thread pool.Code:
Private void button2_click (Object sender, eventargs E)
{
Int maxth, minth, portth;

Threadpool. getmaxthreads (Out maxth, out portth );
Threadpool. getminthreads (Out minth, out portth );
Richtextbox1.text = maxth. tostring () + "," + minth. tostring () + "," + portth. tostring () + "\ n ";

Threadpool. queueuserworkitem (New waitcallback (A1), 1 );
Threadpool. queueuserworkitem (New waitcallback (A2), 1 );
}

Private void A1 (object I)
{
String STR = I. tostring ();
If (this. invokerequired)
{
Updaterichtxt ur = new updaterichtxt (updatert );
This. Invoke (UR, STR );
}
}
Private void A2 (object I)
{
String STR = (INT) I + 1). tostring ();
If (this. invokerequired)
{
Updaterichtxt ur = new updaterichtxt (updatert );
This. Invoke (UR, STR );
}
}

Private delegate void updaterichtxt (string itxt );

Private void updatert (string itxt)
{
Richtextbox1.text = richtextbox1.text + itxt + "\ n ";
}
Here you should be clear about the correct application of multithreading.

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.