FAQ Database Discussion Community

How do I cancel a concurrent heavy Task?

I have a Task by heavy process runs in that's body. In addition, we have no access to the body of this method (heavy process) and we must wait until to completion the process. Now my question is, how do I cancel without interrupting the task so that I do...

Async/await with CancellationToken doesn't cancel the operation

I want to use the CancellationToken to abort a file download. This is what I tried: public async Task retrieveDocument(Document document) { // do some preparation work first before retrieving the document (not shown here) if (cancelToken == null) { cancelToken = new CancellationTokenSource(); try { Document documentResult = await...

CancellationTokenSource vs. volatile boolean

Are there any benefits for using a CancellationTokenSource over a volatile boolean field for signalling a Task to finish?

Why is the task is not cancelled when I call CancellationTokenSource's Cancel method in async method?

I created a small wrapper around CancellationToken and CancellationTokenSource. The problem I have is that the CancelAsync method of CancellationHelper doesn't work as expected. I'm experiencing the problem with the ItShouldThrowAExceptionButStallsInstead method. To cancel the running task, it calls await coordinator.CancelAsync();, but the task is not cancelled actually and doesn't...

What is the proper way to delay with cancellation support inside of a task execution delegate?

I don't see any specific mention either on MSDN or here about how to accomplish this. The use case is somewhat obscure, but still valid I suspect. var cancel = new CancellationTokenSource(); var task = Task.Factory.StartNew(() => { Task.Delay(1000, cancel.Token).Wait(); }, cancel.Token); cancel.CancelAfter(100); task.Wait(); The above code will attempt to...