cancellation-token

How can I cancel a Task without LOOP

二次信任 提交于 2019-12-13 09:04:23
问题 Hi I've been reading alot on the forum but I'm not able to find the answer to my problem... Here's my function that I want to cancel when a boolean turn to TRUE: Task<PortalODataContext> task = Task.Factory.StartNew(() => { var context = connection.ConnectToPortal(); connection.ListTemplateLib = this.ShellModel.ConnectionManager.GetTemplateLibrarys(connection); connection.ListTemplateGrp = this.ShellModel.ConnectionManager.GetTemplateGroups(connection, connection.TemplateLibraryId);

Can a client cancel a Web Api request?

若如初见. 提交于 2019-12-12 16:26:19
问题 I have the following method in my web api controller (the long running methods honor cancellation tokens, i.e. they won't run if cancellation has been requested): public async Task<IHttpActionResult> ApiMethod(CancellationToken cancellationToken) { await LongRunningNetworkOperation1(cancellationToken); await LongRunningNetworkOperation2(cancellationToken); return Ok(); } The client has the following code to call my web api controller: using (var httpClient = new HttpClient()) { httpClient

c# lock and listen to CancellationToken

一个人想着一个人 提交于 2019-12-12 08:33:28
问题 I want to use lock or a similar synchronization to protect a critical section. At the same time I want to listen to a CancellationToken. Right now I'm using a mutex like this, but mutex doesn't have as good performance. Can I use any of other synchronization classes (including the new .Net 4.0) instead of the mutex? WaitHandle.WaitAny(new[] { CancelToken.WaitHandle, _mutex}); CancelToken.ThrowIfCancellationRequested(); 回答1: Take a look at the new .NET 4.0 Framework feature SemaphoreSlim Class

Async.Start with timeout and cancellationToken?

断了今生、忘了曾经 提交于 2019-12-11 08:07:45
问题 I have an Async<'T> computation that I want to run, and obtain the result 'T . I only have two requirements: After certain timeout:TimeSpan has passed, I want the computation/IO to be aborted. I want to run it with a cancellationToken just in case I want to abort it before timeout has passed. According to my requirement (1) above, you might think that Async.StartChild is a good candidate because it accepts a timeout parameter, however, it doesn't accept a cancellationToken parameter! It seems

How can a default(CancellationToken) have a corresponding CancellationTokenSource

℡╲_俬逩灬. 提交于 2019-12-10 02:58:11
问题 When I create a default CancellationToken I can see in the debugger that the CancellationToken has a CancellationTokenSource associated with it which is stored in the private m_source field: I am wondering how can that be as for structs the default keyword "will return each member of the struct initialized to zero or null depending on whether they are value or reference types" and CancellationTokenSource is a reference type. CancellationToken does have 2 constructors that set this field

F# Async.RunSynchronously with timeout and cancellationToken

社会主义新天地 提交于 2019-12-09 18:32:03
问题 When calling Async.RunSynchronously with a timeout and a CancellationToken, the timeout value seems to be ignored. I can work around this by calling CancelAfter on the CancellationToken, but ideally I'd like to be able to distinguish between exceptions that occur in the workflow, TimeOutExceptions and OperationCanceledExceptions. I believe the sample code below demonstrates this. open System open System.Threading let work = async { let endTime = DateTime.UtcNow.AddMilliseconds(100.0) while

Is catching TaskCanceledException and checking Task.Canceled a good idea?

限于喜欢 提交于 2019-12-09 07:50:29
问题 There are some people on my team who really love coding with async Task . And sometimes they like to use CancellationToken parameters. What I'm unsure about is whether we should as a team be using this style of code (A): async Task<someObject> DoStuff(CancellationToken t) { while (!t.IsCanceled) { try { Task.Delay(5000, t); } catch (AggregateException e) // or is it TaskCanceledException or OperationCanceledException? I don't know? :) { } // poll something, return someObject, or null } return

How to get effect of Task.WhenAny for a Task and CancellationToken?

本小妞迷上赌 提交于 2019-12-07 10:18:47
问题 I have interactive task which in "worst" scenario is not executed at all, thus it is represented by TaskCompletionSource . I would like to wait for either this task completes, or token which I received is cancelled -- whichever happens first. Perfect tool for such job would be Task.WhenAny , the only problem is it takes only tasks, and I have one Task and one CancellationToken . How to wait (asynchronously, like Task.WhenAny ) for the first event triggered -- completed task, or cancelled

Is default(CancellationToken) equivalent to CancellationToken.None?

孤街醉人 提交于 2019-12-06 16:38:24
问题 Looking at the implementation of CancellationToken.None , it is simply returning default(CancellationToken) . However, I see no reference in CancellationToken 's documentation that the two are equivalent. I'd like to offer an API like this but not until I'm sure it'll always work: Task DoSomething(CancellationToken token = default(CancellationToken)) Is it defined behavior that default(CancellationToken) is the same as CancellationToken.None , or is this just an implementation detail? 回答1:

How do I cancel a Blocked Task in C# using a Cancellation Token?

杀马特。学长 韩版系。学妹 提交于 2019-12-05 18:50:44
I have a Task which is always blocked and I have a CancellationToken passed into it which is used to cancel the task. However the Continuation task is never executed which is set to execute on Task's cancellation. The code is: _tokenSrc = new CancellationTokenSource(); var cnlToken = _tokenSrc.Token; Task.Run(() => // _stream.StartStream() blocks forever _stream.StartStream(), cnlToken) .ContinueWith(ant => { _logger.Warn("Stream task cancellation requested, stopping the stream"); _stream.StopStream(); _stream = null; _logger.Warn("Stream stopped and task cancelled"); },