How to “sleep” until timeout or cancellation is requested

I just blogged about it here: CancellationToken and Thread.Sleep in Short: var cancelled = token.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)); In your context: void MyFunc (CancellationToken ct) { //… // simulate some long lasting operation that should be cancelable var cancelled = ct.WaitHandle.WaitOne(TimeSpan.FromSeconds(10)); }

Cancelling an HttpClient Request – Why is TaskCanceledException.CancellationToken.IsCancellationRequested false?

That’s the case because HttpClient internally (in SendAsync) is using a TaskCompletionSource to represent the async operation. It returns TaskCompletionSource.Task and that’s the task you await on. It then calls base.SendAsync and registers a continuation on the returned task that cancels/completes/faults the TaskCompletionSource‘s task accordingly. In the case of cancellation it uses TaskCompletionSource.TrySetCanceled which associates … Read more

How to combine TaskCompletionSource and CancellationTokenSource?

If I understand you correctly, you can do it like this: using (cancellationToken.Register(() => { // this callback will be executed when token is cancelled task_comletion_source.TrySetCanceled(); })) { // … await task_comletion_source.Task; } Note that it will throw an exception on your await, which you have to handle.

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

Linking Cancellation Tokens

You want to use CancellationTokenSource.CreateLinkedTokenSource. It allows to have a “parent” and a “child” CancellationTokenSourcees. Here’s a simple example: var parentCts = new CancellationTokenSource(); var childCts = CancellationTokenSource.CreateLinkedTokenSource(parentCts.Token); childCts.CancelAfter(1000); Console.WriteLine(“Cancel child CTS”); Thread.Sleep(2000); Console.WriteLine(“Child CTS: {0}”, childCts.IsCancellationRequested); Console.WriteLine(“Parent CTS: {0}”, parentCts.IsCancellationRequested); Console.WriteLine(); parentCts.Cancel(); Console.WriteLine(“Cancel parent CTS”); Console.WriteLine(“Child CTS: {0}”, childCts.IsCancellationRequested); Console.WriteLine(“Parent CTS: {0}”, parentCts.IsCancellationRequested); Output … Read more

How to reset a CancellationToken properly?

You need to recreate the CancellationTokenSource – there is no way to “reset” this once you set it. This could be as simple as: private void Button_Click(object sender, RoutedEventArgs e) { if (button.Content == “Start”) { button.Content = “Stop”; cts.Dispose(); // Clean up old token source…. cts = new CancellationTokenSource(); // “Reset” the cancellation token … Read more

How to “sleep” until timeout or cancellation is requested in .NET 4.0

I just blogged about it here: CancellationToken and Thread.Sleep in Short: var cancelled = token.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)); In your context: void MyFunc (CancellationToken ct) { //… // simulate some long lasting operation that should be cancelable var cancelled = ct.WaitHandle.WaitOne(TimeSpan.FromSeconds(10)); }

How to cancel a CancellationToken

As the documentation states, you need to call the Cancel() method from the token source, not the token itself. Note the example code in the CancellationToken Struct documentation: // Define the cancellation token. CancellationTokenSource source = new CancellationTokenSource(); CancellationToken token = source.Token; … source.Cancel(); how can I, in possession of only a CancellationToken, cancel it? … Read more

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

When to dispose CancellationTokenSource?

Speaking about whether it’s really necessary to call Dispose on CancellationTokenSource… I had a memory leak in my project and it turned out that CancellationTokenSource was the problem. My project has a service, that is constantly reading database and fires off different tasks, and I was passing linked cancellation tokens to my workers, so even … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)