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:
- The thread must run at a non-normal thread priority.
- The thread needs to be represented as a foreground thread
- A computing-restricted task takes a long time to run
- 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.