. Net multi-thread

Source: Internet
Author: User

In project development, it is often difficult to deal with one type of problems: the calculation of data is relatively large, and the operation time is relatively large. For this type of problem, the general solution is to create a dedicated thread for processing, so that the user can continue other operations, the system will not display the effect of the card.

The following rules are generally used to create a dedicated thread to execute asynchronous computing restrictions:

    1. The thread must run at a non-normal thread priority.
    2. The thread needs to be represented as a foreground thread
    3. A computing-restricted task takes a long time to run
    4. To start a thread and call the abort method of the thread to terminate it in advance

Create a dedicated thread to construct an instance of the system. Threading. Thread class and pass the method name to its constructor. The following is the constructor of thread:

 

[Securitysafecritical]PublicThread (parameterizedthreadstart start); [securitysafecritical]PublicThread (threadstart start); [securitysafecritical]PublicThread (parameterizedthreadstart start,IntMaxstacksize); [securitysafecritical]PublicThread (threadstart start,IntMaxstacksize );

The first parameter of the above four constructor is the delegate, that is, the callback function entry. The delegate type is defined as follows:

     //  Summary:  // Represents the method that executes on a system. Threading. thread.  //      //  Parameters:  //  OBJ:  //  An object that contains data for the thread procedure. [Comvisible ( False  )] Public Delegate VoidParameterizedthreadstart (ObjectOBJ );  //  Summary:  //  Represents the method that executes on a system. Threading. thread. [Comvisible ( True  )]  Public   Delegate   Void Threadstart ();

The following uses the parameterizedthreadstart delegate as an example to describe how:

Class  Program {  Static   Void Main ( String  [] ARGs) {console. writeline (  "  Start main thread...  "  );  //  The definition of computesum must be consistent with that of the parameterizedthreadstart delegate. Thread dedicatedthread = New  Thread (computesum ); //  The parameter of the Start method is the parameter passed to the delegate call method. Dedicatedthread. Start ( 5  ); Console. writeline (  "  Doing other work here...  "  ); Thread. Sleep (  5000  );  //  The join method causes the calling thread to block any currently executedCodeUntil the thread that calls the join method is destroyed or terminated. You can pass the waiting time limit value to the join method. Dedicatedthread. Join (); console. writeline (  "  Hit <enter> to end this program...  "  ); Console. Readline ();}  Private   Static   Void Computesum ( Object  State ){  Int Number = ( Int  ) State; Int Sum = 0  ;  For ( Int Data = 0 ; Data <number; Data ++ ) {Sum + = Data;} console. writeline (  "  Sum is {0}  "  , Sum); thread. Sleep (  1000 );}} 

In fact, we should use the CLR thread pool to execute asynchronous computing restrictions, because the thread pool automatically creates and destroys the management thread for you. The thread pool creates a set of threads for the use of various tasks. Therefore, we should try to use the CLR thread pool.

 

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.