Why CancellationToken is separate from CancellationTokenSource?

I was involved in the design and implementation of these classes. The short answer is “separation of concerns“. It is quite true that there are various implementation strategies and that some are simpler at least regarding the type system and initial learning. However, CTS and CT are intended for use in a great many scenarios … Read more

Using CancellationToken for timeout in Task.Run does not work [duplicate]

Cancellation in Managed Threads: Cancellation is cooperative and is not forced on the listener. The listener determines how to gracefully terminate in response to a cancellation request. You didn’t write any code inside your Task.Run method to access your CancellationToken and to implement cancellation – so you effectively ignored the request for cancellation and ran … Read more

NetworkStream.ReadAsync with a cancellation token never cancels

I finally found a workaround. Combine the async call with a delay task (Task.Delay) using Task.WaitAny. When the delay elapses before the io task, close the stream. This will force the task to stop. You should handle the async exception on the io task correctly. And you should add a continuation task for both the … Read more

How to use the CancellationToken without throwing/catching an exception?

You can implement your work method as follows: private static void Work(CancellationToken cancelToken) { while (true) { if(cancelToken.IsCancellationRequested) { return; } Console.Write(“345”); } } That’s it. You always need to handle cancellation by yourself – exit from method when it is appropriate time to exit (so that your work and data is in consistent state) … Read more

Async network operations never finish

So i’ve made an extension method on IDisposable that creates a CancellationToken that disposes the connection on timeout, so the task finishes and everything carries on: public static IDisposable CreateTimeoutScope(this IDisposable disposable, TimeSpan timeSpan) { var cancellationTokenSource = new CancellationTokenSource(timeSpan); var cancellationTokenRegistration = cancellationTokenSource.Token.Register(disposable.Dispose); return new DisposableScope( () => { cancellationTokenRegistration.Dispose(); cancellationTokenSource.Dispose(); disposable.Dispose(); }); } … Read more

How to use the CancellationToken property?

You can implement your work method as follows: private static void Work(CancellationToken cancelToken) { while (true) { if(cancelToken.IsCancellationRequested) { return; } Console.Write(“345”); } } That’s it. You always need to handle cancellation by yourself – exit from method when it is appropriate time to exit (so that your work and data is in consistent state) … Read more

Cancellation token in Task constructor: why?

Passing a CancellationToken into the Task constructor associates it with the task. Quoting Stephen Toub’s answer from MSDN: This has two primary benefits: If the token has cancellation requested prior to the Task starting to execute, the Task won’t execute. Rather than transitioning to Running, it’ll immediately transition to Canceled. This avoids the costs of … Read more