cancellation

How is an observable subscription gracefully terminated?

冷暖自知 提交于 2021-02-08 15:43:12
问题 I'm attempting to use Reactive Extensions (Rx) to process a stream of data. The processing of each element may take some time, though. To break the processing, I'm using a CancellationToken , which effectively stops the subscription. When cancel has been requested, how do I gracefully finish the current work and terminate properly without losing any data? Example var cts = new CancellationTokenSource(); cts.Token.Register(() => Console.WriteLine("Token cancelled.")); var observable =

Task cancellation with async task

旧时模样 提交于 2021-02-08 05:27:34
问题 I'm trying to make use of cancellation tokens as described in this FAQ. This was my initial thought: private async void OnLoginButtonClicked(object sender, EventArgs e) { if (this.cancelToken == null) { this.cancelToken = new CancellationTokenSource(); } try { bool loginSuccess = await AsyncLoginTask(this.cancelToken.Token); if (loginSuccess) { // Show main page } } catch (OperationCanceledException ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } catch (Exception ex) { System

How to cancel all remaining tasks in gather if one fails?

不打扰是莪最后的温柔 提交于 2021-02-07 05:10:23
问题 In case one task of gather raises an exception, the others are still allowed to continue. Well, that's not exactly what I need. I want to distinguish between errors that are fatal and need to cancel all remaining tasks, and errors that are not and instead should be logged while allowing other tasks to continue. Here is my failed attempt to implement this: from asyncio import gather, get_event_loop, sleep class ErrorThatShouldCancelOtherTasks(Exception): pass async def my_sleep(secs): await

How to cancel all remaining tasks in gather if one fails?

廉价感情. 提交于 2021-02-07 05:07:41
问题 In case one task of gather raises an exception, the others are still allowed to continue. Well, that's not exactly what I need. I want to distinguish between errors that are fatal and need to cancel all remaining tasks, and errors that are not and instead should be logged while allowing other tasks to continue. Here is my failed attempt to implement this: from asyncio import gather, get_event_loop, sleep class ErrorThatShouldCancelOtherTasks(Exception): pass async def my_sleep(secs): await

Why does the OnlyOnCanceled continuation get called?

元气小坏坏 提交于 2021-02-05 07:23:10
问题 When calling await RunAsync(); on the below code, I would expect the continuation with TaskContinuationOptions.OnlyRanToCompletion continuation to run, however the OnlyOnCanceled continuation gets called (yielding the debug output "Task canceled"). Why? private static async Task RunAsync() { try { await Task.Run(() => DoWork()) .ContinueWith( (t) => { if (t?.Exception != null) { throw t.Exception; } }, TaskContinuationOptions.OnlyOnFaulted ).ContinueWith( (t) => { Debug.WriteLine("Task

What would be a good way to Cancel long running IO/Network operation using Tasks?

半腔热情 提交于 2021-02-04 18:25:17
问题 I've been studying Tasks in .net 4.0 and their cancellation. I like the fact that TPL tries to deal with cancellation correctly in cooperative manner. However, what should one do in situation where a call inside a task is blocking and takes a long time? For examle IO/Network. Obviously cancelling writes would be dangerous. But those are examples. Example: How would I cancel this? DownloadFile can take a long time. Task.Factory.StartNew(() => WebClient client = new WebClient(); client

What would be a good way to Cancel long running IO/Network operation using Tasks?

假装没事ソ 提交于 2021-02-04 18:25:13
问题 I've been studying Tasks in .net 4.0 and their cancellation. I like the fact that TPL tries to deal with cancellation correctly in cooperative manner. However, what should one do in situation where a call inside a task is blocking and takes a long time? For examle IO/Network. Obviously cancelling writes would be dangerous. But those are examples. Example: How would I cancel this? DownloadFile can take a long time. Task.Factory.StartNew(() => WebClient client = new WebClient(); client

Cooperative cancellation in F# with cancel continuation

自古美人都是妖i 提交于 2021-01-27 17:50:31
问题 Probably I have here 2 questions instead of one, but anyway. I'm implementing cooperative cancellation as here suggested. Here is my test code: type Async with static member Isolate(f : CancellationToken -> Async<'T>) : Async<'T> = async { let! ct = Async.CancellationToken let isolatedTask = Async.StartAsTask(f ct) return! Async.AwaitTask isolatedTask } let testLoop (ct: CancellationToken) = async { let rec next ix = if ct.IsCancellationRequested then () else printf "%i.." ix Thread.Sleep 10