C # CancellationTokenSource terminating thread CancellationTokenSource implementation of cancellation of timeout task

Source: Internet
Author: User

C # uses CancellationTokenSource to terminate threads

Using the CancellationTokenSource object needs to be used in conjunction with the Task object, and the task will control the state of the current run (this does not concern us with how the hole is controlled). The CancellationTokenSource is the external control of the task, such as cancellation and timing cancellation.

Let's take a look at the sample code

  1. class Program
  2. {
  3. //Declare CancellationTokenSource object
  4. static cancellationtokensource canceltokensource = new CancellationTokenSource ();
  5. //Program Entry
  6. static void Main(string[] args)
  7. {
  8. Task. Factory. StartNew(mytask, canceltokensource. Token);
  9. Console. WriteLine("Please press ENTER to stop");
  10. Console. ReadLine();
  11. Canceltokensource. Cancel();//Notify Cancel task or terminate thread
  12. Console. WriteLine("stopped");
  13. Console. ReadLine();
  14. }
  15. //test method
  16. static void mytask()
  17. {
  18. //Decide whether to cancel the task
  19. while (! Canceltokensource. iscancellationrequested)
  20. {
  21. Console. WriteLine(DateTime. Now);
  22. Thread. Sleep(+);
  23. }
  24. }
  25. }

Cancellation of timeout tasks via CancellationTokenSource in. Net 4.5

We typically use CancellationTokenSource to implement task cancellation during task-based tasks, starting with a simple example.

();

. Factory.startnew (() =
{
(!canceltokensource.iscancellationrequested)
{
. Now);
. Sleep (1000);
}
}, Canceltokensource.token);

);
. ReadLine ();
Canceltokensource.cancel ();
);
. ReadLine ();

Many times, in addition to the manual cancellation as in the example above, we tend to set an expected execution time on the task, and automatically cancel the timeout task. The usual practice is to start a new timer and execute the Cancellationtokensource.cancel method in the timer's timeout callback. In. Net 4.5, this is further simplified, and we can do this by setting timeouts when creating CancellationTokenSource.

var canceltokensource = new CancellationTokenSource (3000);

In addition, the same effect can be achieved with the following code.

Canceltokensource.cancelafter (3000);

C # CancellationTokenSource terminating thread CancellationTokenSource implementation of cancellation of timeout task

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.