This example describes the implementation of a C # implementation to terminate an executing thread, and provides an in-depth analysis of some error-prone areas, as follows:
In general, many people will use the Abort method to terminate the thread, in fact, this practice is not advisable! If your thread is working on a critical resource, there is a risk that the resource will not be properly released and there is a deadlock problem. The correct approach should be to use tags to terminate the execution of the thread.
The basic idea is to define a variable that describes the stop signal and set the variable to false before the entire program starts. In the thread, the loop determines whether the variable is set to True, and if not, resumes execution, or exits the loop and frees the resource, then exits execution. When we need the thread to exit, just set this "stop" signal to true.
Let's take a look at the specific steps.
First define a "stop" signal variable:
Private volatile bool CanStop = false;
Note that we use the volatile keyword here, because the CanStop variable will be used by both the calling thread and the execution thread, initializing it in the calling thread and setting its value, while judging its value in the execution thread. This tells the compiler that the CanStop variable will be used by multiple threads, forcing the compiler to not optimize its state. If you are interested, you can see more explanations for this volatile keyword on MSDN. The CanStop is also initialized here.
Now let's look at the code for thread creation and execution:
i = 0;//uses an anonymous method to define the thread's execution body thread thread = new Thread (delegate (object param) { //waits for a "stop" signal and executes while (!) if no signal is received . CanStop) { i++; Updatelabel (i); } At this point, you have received a stop signal where you can release the resource and //Initialize the variable canStop = false;}); Thread. Start ();
Very simply, the execution of the thread repeatedly determines whether the CanStop variable is true, if you immediately jump out of the while loop (stop the variable's self-addition and update the interface operation), and then reinitialize the CanStop variable to false for the next use.
I hope this article is helpful to everyone's C # programming.
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
C # Implementation terminates a thread that is executing
This address: http://www.paobuke.com/develop/c-develop/pbk23541.html
Related Content C # programming call Cards.dll Implementing a graphical licensing feature example C # ListView Click on the header to sort the data by the Implementation code C # system time and UNIX timestamps convert each other easily learn C # boxing and unboxing
C # Programming implements rounding and redundancy methods. net2.0+ WinForm Project implement pop-up container layer C # Brush pen Draw Curve Method C # Attribute extension method
C # Implementation terminates a thread that is executing