C # Implementation terminates a thread that is executing

Source: Internet
Author: User

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 # implementing asynchronous Programming in C # using GET, Post requests to get results Ngui implementing a sliding page-turn effect instance code in combination with the. NET Framework to trigger base class events and implement Interface events in C # derived classes
C # Predefined data type value types and reference types Introduction C # Custom RSA Cryptographic decryption and RSA signature and validation class instances WinForm custom functions FindControl implementation by name lookup Control C # leverages reflection to determine whether an object contains an attribute

C # Implementation terminates a thread that is executing

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.