task-parallel-library

Ignore the Tasks throwing Exceptions at Task.WhenAll and get only the completed results

青春壹個敷衍的年華 提交于 2020-05-25 10:25:02
问题 I am working on a Task parallel problem that I have many Tasks that may or may not throw Exception. I want to process all the tasks that finishes properly and log the rest. The Task.WhenAll propage the Task exception without allowing me to gather the rest results. static readonly Task<string> NormalTask1 = Task.FromResult("Task result 1"); static readonly Task<string> NormalTask2 = Task.FromResult("Task result 2"); static readonly Task<string> ExceptionTk = Task.FromException<string>(new

How to return AggregateException from async method

余生长醉 提交于 2020-05-24 04:07:09
问题 I got an async method working like an enhanced Task.WhenAll . It takes a bunch of tasks and returns when all are completed. public async Task MyWhenAll(Task[] tasks) { ... await Something(); ... // all tasks are completed if (someTasksFailed) throw ?? } My question is how do I get the method to return a Task looking like the one returned from Task.WhenAll when one or more tasks has failed? If I collect the exceptions and throw an AggregateException it will be wrapped in another

Task.WaitAll, how to find the tasks causing AggregateException

限于喜欢 提交于 2020-05-11 06:40:53
问题 Let's say that I got the following code: var tasks = BuildTaskList(); try { Task.WaitAll(tasks.ToArray()); } catch (AggregateException exception) { } How do I know which task threw which of the exceptions in exception.InnerExceptions ? 回答1: You still have the list of Tasks , and each Task has an Exception property. Using that you can figure out which exceptions belong with which Task . But, if you can, it'd be better to use Task.WhenAll or TaskFactory.ContinueWhenAll than do a blocking Wait.

Task.WaitAll, how to find the tasks causing AggregateException

非 Y 不嫁゛ 提交于 2020-05-11 06:40:44
问题 Let's say that I got the following code: var tasks = BuildTaskList(); try { Task.WaitAll(tasks.ToArray()); } catch (AggregateException exception) { } How do I know which task threw which of the exceptions in exception.InnerExceptions ? 回答1: You still have the list of Tasks , and each Task has an Exception property. Using that you can figure out which exceptions belong with which Task . But, if you can, it'd be better to use Task.WhenAll or TaskFactory.ContinueWhenAll than do a blocking Wait.

Task.WaitAll, how to find the tasks causing AggregateException

人盡茶涼 提交于 2020-05-11 06:38:09
问题 Let's say that I got the following code: var tasks = BuildTaskList(); try { Task.WaitAll(tasks.ToArray()); } catch (AggregateException exception) { } How do I know which task threw which of the exceptions in exception.InnerExceptions ? 回答1: You still have the list of Tasks , and each Task has an Exception property. Using that you can figure out which exceptions belong with which Task . But, if you can, it'd be better to use Task.WhenAll or TaskFactory.ContinueWhenAll than do a blocking Wait.

What is the ValueTask equivalent of Task.CompletedTask?

删除回忆录丶 提交于 2020-05-09 05:21:50
问题 I am implementing IAsyncDisposable which requires me to return a ValueTask , but sometimes my dispose method has nothing to do. How should I return in this case? At the moment I'm returning new ValueTask(Task.CompletedTask) which seems to work but since the point of valueTasks is to avoid creating unnecessary heap objects, I'm sure there should be a simpler and more efficient way. 回答1: All structs have a default constructor. The default constructor of ValueTask creates a completed ValueTask :

How can I await an array of tasks and stop waiting on first exception?

大兔子大兔子 提交于 2020-04-27 04:34:28
问题 I have an array of tasks and I am awaiting them with Task.WhenAll. My tasks are failing frequently, in which case I inform the user with a message box so that she can try again. My problem is that reporting the error is delayed until all tasks are completed. Instead I would like to inform the user as soon as the first task has thrown an exception. In other words I want a version of Task.WhenAll that fails fast. Since no such build-in method exists I tried to make my own, but my implementation

TPL cancellation continuation never called on cancelled Task

我只是一个虾纸丫 提交于 2020-04-12 20:51:36
问题 I have the following setup in my code that utilizes the TPL: One field in my class: private CancellationTokenSource _cancellationTokenSource; This CancellationTokeSource gets instatiated every time I create a TPL task that uses that particular cancellationtoken The actualy TPL tasks look like this: var dataRetrievalTask = new Task<List<myType>>(() => { // retrieve data and do something foreach (var a in retrievalMethod()) { if (_cancellationTokenSource.Token.IsCancellationRequested)

TPL cancellation continuation never called on cancelled Task

十年热恋 提交于 2020-04-12 20:43:31
问题 I have the following setup in my code that utilizes the TPL: One field in my class: private CancellationTokenSource _cancellationTokenSource; This CancellationTokeSource gets instatiated every time I create a TPL task that uses that particular cancellationtoken The actualy TPL tasks look like this: var dataRetrievalTask = new Task<List<myType>>(() => { // retrieve data and do something foreach (var a in retrievalMethod()) { if (_cancellationTokenSource.Token.IsCancellationRequested)

Exception thrown from task is swallowed if thrown after 'await'

送分小仙女□ 提交于 2020-04-11 02:06:09
问题 I'm writing a background service using .net's HostBuilder. I have class called MyService that implements BackgroundService ExecuteAsync method and i encountered some weird behavior there. Inside the method i await a certain task and any exception thrown after the await is swallowed but an exception that is thrown before the await terminates the process. I looked online in all sorts of forums (stack overflow, msdn, medium) but I could not find an explanation for this behavior. public class