Asynchronously interrupt C # TPL tasks

Is there a way to asynchronously interrupt C # TPL tasks created using Task.Factory.Create(() => {stuff}); ? I saw that there is a way to do this using the CancellationToken , but I like to avoid validation with IsCancellationRequested .

0
source share
2 answers

EDIT: Updated for .NET 4.5 and later, more at the bottom of the post.


CancellationToken the thing you want to use. Checking IsCancellationRequested may look like a pain, but it provides a clean way to handle cancellation, as opposed to creating a thread and then aborting it and the need to handle the thread interruption exception in all code that runs in parallel.

 var cts = new CancellationTokenSource(); var token = cts.Token; var task = Task.Run(() => { // Do Job Step 1... if (token.IsCancellationRequested) { // Handle cancellation here, if necessary. // Thanks to @svick - this will set the Task status to Cancelled // by throwing OperationCanceledException inside it. token.ThrowIfCancellationRequested(); } // Do Job Step 2... // ... }, token); // Later, to kill all the tasks and their children, simply: cts.Cancel(); 

This may add some confusion to the delegate, but the revocation itself is so simple that I never needed to use anything coarser than the cancellation token source. You did not say why you would like to avoid this, so if you do not have a hard limit, I would stick to the cancellation markers.


As with @ipavlu's comment, for a simple use case, it is much better to use Task.Run instead of the original Task.Factory.StartNew to disable asynchronous operation. They provide the same basic functionality, but Task.Run has safer defaults and better support for new async functions, and Task.Factory.StartNew can be used to indicate advanced behavior. More information here: https://blogs.msdn.microsoft.com/pfxteam/2011/10/24/task-run-vs-task-factory-startnew/

+8
source

Cancellation can be a difficult task, but mainly because cancellation, like parallel processing in general, is complex. You almost certainly do not want to allow interruption at arbitrary points in your code, since this creates almost so many new possible boundary cases for checking, since there are lines of your code in your asynchronously running method. This is a nightmare scenario that leads to rare accidents in the wild that are almost impossible to reproduce.

Part of designing your asynchronous process is the decision where it is safe to cancel and how it should work with cancellation. It can also mean identifying the blocking API calls you make (such as network I / O) and invoking the appropriate methods to interrupt these blocking operations. Correct cancellation support requires a lot of preliminary project effort to properly support it, but if this feature is required, upfront investments will more than pay off with reduced headaches later. This means, however, that you want to be conservative in budgeting for cancellation as a function, as it is rarely as simple as it seems.

+7
source

Source: https://habr.com/ru/post/948332/


All Articles