Task_ canceling asynchronous compute throttling operations CancellationTokenSource and catching exceptions in asynchronous threads

Source: Internet
Author: User

Why:threadpool has no built-in mechanism to mark when the current thread is finished, and there is no mechanism to get the return value when the operation is complete, thus launching a task to manage asynchronous threads more precisely.

How: Control how to create a task by constructing the parameter taskcreationoptions of the method to see the enumeration value of the enumeration. The result of getting a task execution has the Task.result property, which calls Wait () internally, such as WaitAny (), WaitAll (), and so on.

How do I cancel a task in execution?

Instantiate the Cancelletiontokensource class, pass the object as a parameter to the Async method, call the object's throwifcancellationrequested () in the Async method, and if the Cancel () method of the object is called, Then the Async method throws an exception operationcanceledexception, and in the main thread is captured with Try-catch (aggregateexception), the exception can be caught and processed.

Task_ canceling asynchronous compute throttling operations CancellationTokenSource and catching exceptions in asynchronous threads

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.