. NET4.0 multithreaded Programming---cooperative cancellation

Source: Internet
Author: User

In multithreaded programming the midline pool is to be mentioned, Before. net4.0 in general for the thread pool is that the required thread to the thread pool, we ourselves will be more focused on the business, the personal think that this is not only the advantages of the thread pool is also a disadvantage---after joining the thread pool, there is no human control, there is no intrinsic way to inform the execution results of the thread.

I read Jeffrey's book 26th this morning. According to your own understanding, today we mainly learn how to cancel threads in a thread pool.

  The two classes below the System.thread namespace are introduced first:

    • CancellationToken, in fact, is a structure that primarily propagates notifications about actions that should be canceled.
    • CancellationTokenSource, which is a class whose primary function is to manipulate (cancel) the thread associated with it, and then notify CancellationToken that its corresponding operation is canceled.
  CancellationTokenSource mainly describes a method and two properties:
    • Token, this property gets the CancellationToken associated with the CancellationTokenSource.
    • IsCancellationRequested, this property returns whether the cancel operation has been performed.
    • Cancel, which is a method that is used to perform a cancel action
    • Detailed parameters can be viewed in specific functions
   CancellationToken describes a method and a property
    • Register method for registering a delegate that executes when the current CancellationToken is canceled
    • IsCancellationRequested, which is used primarily to show whether the cancellation is performed for the current CancellationToken
    • Detailed parameters can be viewed in specific functions
To implement an example based on the above introduction, make a slight change to Mr Jeffrey's example:Code namespace cancellationstudy { internal class Cancellationdemo { public Static C10>void Main () {System.Threading.CancellationTokenSource cts = NewCancellationTokenSource ();//Pass the CancellationToken to the appropriate action ThreadPool.QueueUserWorkItem (o=Count (CTS. Token,1000)); ThreadPool.QueueUserWorkItem (o=Count2 (CTS. Token,1000));
Console.WriteLine ("Please <Enter> to cancel the operation."); Console.ReadLine (); Cts. Cancel (); Console.ReadLine (); }
Private Static voidCount (CancellationToken token,IntCountto) {For(IntCount= 0; Count<Countto; Count++) {//This iscancellationrequested is a CancellationToken property.//Used primarily to show whether cancellation is performed for the current token If(token.) iscancellationrequested) {Console.WriteLine ("Count is cancelled");Break; } Console.WriteLine (count); Thread.Sleep (200); } Console.WriteLine ("Count is done"); }
Private Static voidCount2 (CancellationToken token,IntCountto) {For(IntCount= 0< Countto; Count++if "count2 is Cancelled "); break200 "count2 is Done "); } } }

You can execute any method you want at the time of cancellation, when the CancellationToken register method comes in handy.

Code Public Static voidMain () {registermoreoperations ();}
Public Static voidRegistermoreoperations () {var cts1= NewCancellationTokenSource (); Cts1. Token.register (()=Console.WriteLine ("Cts1 Canceled 1")); Cts1. Token.register (()=Console.WriteLine ("Cts1 Canceled 2")); Cts1. Cancel (); Console.WriteLine ( "cts1 cancel=" +cts1. iscancellationrequested); var cts2 = new CancellationTokenSource (); Cts2. Token.register (() => Console.WriteLine ( "cts2 Canceled 1" )); Console.WriteLine ( "cts2 cancel= " + Cts2. iscancellationrequested); }

Above is my own understanding, not very deep, if someone see, also hope pointing. I am very grateful to!!!!!!

At the beginning of this article, it was mentioned that the thread pool had no intrinsic method of informing the thread of execution results. The concept of task is introduced in. net4.0, which can do this entirely. Another day to learn, you have to go to bed today.

. NET4.0 multithreaded Programming---cooperative cancellation

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.