cancellation-token

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

▼魔方 西西 提交于 2019-12-01 17:33:44
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 throw an exception on task.Wait ItWorksWellAndThrowsException seems to be working well and it uses coordinator.Cancel , which is not an async method at all. The question why is the task is not cancelled when I

TaskCancellationException how to avoid the exception on success control flow?

自古美人都是妖i 提交于 2019-12-01 15:56:14
In our application we work a lot with async / await and Tasks. Therefore it does use Task.Run a lot, sometimes with cancellation support using the built in CancellationToken . public Task DoSomethingAsync(CancellationToken cancellationToken) { return Task.Run(() => { while (true) { if (cancellationToken.IsCancellationRequested) break; //do some work } }, cancellationToken); } If i do now cancel the execution using the CancellationToken the execution does stop at the beginning of the next loop, or if the Task did not start at all it throws an exception (TaskCanceledException inside Task.Run).

TaskCancellationException how to avoid the exception on success control flow?

僤鯓⒐⒋嵵緔 提交于 2019-12-01 15:00:33
问题 In our application we work a lot with async / await and Tasks. Therefore it does use Task.Run a lot, sometimes with cancellation support using the built in CancellationToken . public Task DoSomethingAsync(CancellationToken cancellationToken) { return Task.Run(() => { while (true) { if (cancellationToken.IsCancellationRequested) break; //do some work } }, cancellationToken); } If i do now cancel the execution using the CancellationToken the execution does stop at the beginning of the next loop

store cancellation tokens in service fabric services

半腔热情 提交于 2019-12-01 14:22:29
I am trying to achieve cancel tasks feature in service fabric stateful services. Plan is using cancellation token to propagate notices to linked threads / Tasks. The problem is, while there are these long running tasks and threads waiting for this signal, I am not sure how I can find the right cancellation tokens based on another Web API calls. I was thinking of using reliable dictionary, then even before trying it out, I assume this will hit deadend because cancellationToken can't be serialized / deserialized. Please help me what could be good solution to solve this issue. Update (I didn't

store cancellation tokens in service fabric services

故事扮演 提交于 2019-12-01 12:47:14
问题 I am trying to achieve cancel tasks feature in service fabric stateful services. Plan is using cancellation token to propagate notices to linked threads / Tasks. The problem is, while there are these long running tasks and threads waiting for this signal, I am not sure how I can find the right cancellation tokens based on another Web API calls. I was thinking of using reliable dictionary, then even before trying it out, I assume this will hit deadend because cancellationToken can't be

Cancellation Token in await method

落爺英雄遲暮 提交于 2019-12-01 06:00:12
There are many reasons to put a token in the constructor of a task, mentioned here: Cancellation token in Task constructor: why? With the use of keywords, async / await, how is that working? for example my code below: public async Task MethodAsync(CancellationToken token) { await Method01Async(); await Method02Async(); } Although it is an asynchronous process. In no time I used "Task.StartNext" or "Task.Run" or "new Task". To be able to specify my cancellation token, how can I do? You aren't supposed to use the Task constructor in async methods. Usually, you just want to pass the

CancellationTokenSource not behaving as expected

感情迁移 提交于 2019-12-01 03:44:37
问题 what's expected in this case, is that if the user cancels the task by hitting enter, the other task hooked by ContinueWith will run, but it's not the case, as per an AggregateException keeps thrown despite the explicit handling in the ContinueWith which is apparently not being executed. any clarification on the below please? class Program { static void Main(string[] args) { CancellationTokenSource tokensource = new CancellationTokenSource(); CancellationToken token = tokensource.Token; Task

CancellationToken Cancel not breaking out of BlockingCollection

孤街浪徒 提交于 2019-12-01 03:08:40
I have a cancellation token like so static CancellationTokenSource TokenSource= new CancellationTokenSource(); I have a blocking collection like so BlockingCollection<object> items= new BlockingCollection<object>(); var item = items.Take(TokenSource.Token); if(TokenSource.CancelPending) return; When I call TokenSource.Cancel(); The Take does not continue like it should. If I use the TryTake with a poll the Token shows it is being set as Canceled. That's working as expected. If the operation is canceled, items.Take will throw OperationCanceledException . This code illustrates it: static void

CancellationToken Cancel not breaking out of BlockingCollection

拜拜、爱过 提交于 2019-11-30 23:10:47
问题 I have a cancellation token like so static CancellationTokenSource TokenSource= new CancellationTokenSource(); I have a blocking collection like so BlockingCollection<object> items= new BlockingCollection<object>(); var item = items.Take(TokenSource.Token); if(TokenSource.CancelPending) return; When I call TokenSource.Cancel(); The Take does not continue like it should. If I use the TryTake with a poll the Token shows it is being set as Canceled. 回答1: That's working as expected. If the

Stopping a Thread, ManualResetEvent, volatile boolean or cancellationToken

我是研究僧i 提交于 2019-11-29 08:06:04
I have a Thread (STAThread) in a Windows Service, which performs a big amount of work. When the windows service is restarted I want to stop this thread gracefully. I know of a couple of ways A volatile boolean ManualResetEvent CancellationToken As far as I have found out Thread.Abort is a no go... What is the best practice ? The work is perfomed in another class than the one where the thread is started, so it is necessary to either introduce a cancellationToken parameter in a constructor or for example have a volatile variable. But I just can't figure out what is smartest. Update Just to