cancellationtokensource

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

Race condition with CancellationToken where CancellationTokenSource is only cancelled on the main thread

情到浓时终转凉″ 提交于 2019-11-30 23:17:20
Consider a Winforms application, where we have a button that generates some results. If the user presses the button a second time, it should cancel the first request to generate results and start a new one. We're using the below pattern, but we are unsure if some of the code is necessary to prevent a race condition (see the commented out lines). private CancellationTokenSource m_cts; private void generateResultsButton_Click(object sender, EventArgs e) { // Cancel the current generation of results if necessary if (m_cts != null) m_cts.Cancel(); m_cts = new CancellationTokenSource();

Why does cancellation block for so long when cancelling a lot of HTTP requests?

假装没事ソ 提交于 2019-11-30 15:26:47
Background I have some code that performs batch HTML page processing using content from one specific host. It tries to make a large number (~400) of simultaneous HTTP requests using HttpClient . I believe that the maximum number of simultaneous connections is restricted by ServicePointManager.DefaultConnectionLimit , so I'm not applying my own concurrency restrictions. After sending all of the requests asynchronously to HttpClient using Task.WhenAll , the entire batch operation can be cancelled using CancellationTokenSource and CancellationToken . The progress of the operation is viewable via

CancellationTokenSource vs. volatile boolean

故事扮演 提交于 2019-11-30 11:18:24
Are there any benefits for using a CancellationTokenSource over a volatile boolean field for signalling a Task to finish? Of course yes. There are many. I'll list few. CancellationToken supports callbacks. You can be notified when the cancellation is requested. CancellationToken supports WaitHandle which you could wait for indefinitely or with a timeout. You can schedule the cancelation of CancellationToken using CancellationTokenSource.CancelAfter method. You can link your CancellationToken to another, so that when one is cancelled another can be considered as cancelled. By Task if you mean

Task.Factory.FromAsync with CancellationTokenSource

﹥>﹥吖頭↗ 提交于 2019-11-30 08:35:45
I have the following line of code used to read asynchronously from a NetworkStream: int bytesRead = await Task<int>.Factory.FromAsync(this.stream.BeginRead, this.stream.EndRead, buffer, 0, buffer.Length, null); I'd like to make it support cancellation. I see that I can cancel tasks using a CancellationTokenSource , however I don't see any way I can pass it to TaskFactory.FromAsync() . Is it possible to make a FromAsync()-constructed task support cancellation? Edit: I want to cancel a task that is already running. Gigi, unfortunately the semantic nature of FromAsync indicates that you are only

Why does cancellation block for so long when cancelling a lot of HTTP requests?

耗尽温柔 提交于 2019-11-29 22:58:15
问题 Background I have some code that performs batch HTML page processing using content from one specific host. It tries to make a large number (~400) of simultaneous HTTP requests using HttpClient . I believe that the maximum number of simultaneous connections is restricted by ServicePointManager.DefaultConnectionLimit , so I'm not applying my own concurrency restrictions. After sending all of the requests asynchronously to HttpClient using Task.WhenAll , the entire batch operation can be

CancellationTokenSource vs. volatile boolean

送分小仙女□ 提交于 2019-11-29 16:58:16
问题 Are there any benefits for using a CancellationTokenSource over a volatile boolean field for signalling a Task to finish? 回答1: Of course yes. There are many. I'll list few. CancellationToken supports callbacks. You can be notified when the cancellation is requested. CancellationToken supports WaitHandle which you could wait for indefinitely or with a timeout. You can schedule the cancelation of CancellationToken using CancellationTokenSource.CancelAfter method. You can link your

How to cancel an async WCF call?

 ̄綄美尐妖づ 提交于 2019-11-29 12:02:18
I have been trying some time to find out how to implement WCF call cancellation based on the new .NET 4.5 CancellationToken mechanism. All the samples I found are not WCF based, and do not cross the service boundary. My service : [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] public class MyService : IMyService { public void LongOperation() { // do stuff that takes ages... // please cancel me! } } My Client (Desktop App): Using an auto generated proxy : private async void DoLongRunningTaskAsync() { var asyncTask = _myService.LongOperationAsync(); await asyncTask; } How do

Correctly cancel async operation and fire it again

半世苍凉 提交于 2019-11-29 08:08:45
How to handle case, where user might hit the button, which invokes long running async operation, multiple time. My idea was first check if the async operation is running, cancel it and fire it again. So far I have tried to build this kind of functionality using CancellationTokenSource, but it is not working as expected. Some times there is two async operations running, so the "old" async oprations is not cancelled yet when I start new one and this mixes up the resul handling. Any suggestions or examples how to handle this kind of case? public async void Draw() { bool result = false; if (this