First example Task.Run:
Task task1 = Task.Run(() => throw new SystemException("Test exception in Task.Run"));
Task task2 = Task.Run(async () =>
{
await Task.Delay(1000);
WriteToTextBox("Completed");
});
try
{
await Task.WhenAll(task1, task2);
}
catch (Exception ex)
{
WriteToTextBox(ex.Message);
}
This will output:
Completed
Test exception in Task.Run
Second, Task.Run.ContinueWith, exception will be swallowed:
Task task1 = Task.Run(() => throw new SystemException("Test exception in Task.Run"))
.ContinueWith(t => WriteToTextBox("ContinueWith exception test"));
Task task2 = Task.Run(async () =>
{
await Task.Delay(1000);
WriteToTextBox("Completed");
});
try
{
await Task.WhenAll(task1, task2);
}
catch (Exception ex)
{
WriteToTextBox(ex.Message);
}
Output:
ContinueWith exception test
Completed
Parallel.ForEach, exception will be thrown, but not catched outside of Parallel.ForEach, and program will end:
string[] myItems = ["item1", "item2", "item3", "item4", "item5"];
Task task1;
Task task2;
try
{
Parallel.ForEach(myItems, async void (myItem) =>
{
task1 = Task.Run(() => throw new SystemException("Test exception in Task.Run"));
task2 = Task.Run(async () =>
{
await Task.Delay(1000);
WriteToTextBox(myItem);
});
await Task.WhenAll(task1, task2);
});
}
catch (Exception ex)
{
WriteToTextBox(ex.Message);
}
Parallel.ForEachAsync:
string[] myItems = ["item1", "item2", "item3", "item4", "item5"];
Task task1;
Task task2;
using CancellationTokenSource cts = new();
CancellationToken token = cts.Token;
try
{
int i = 0;
await Parallel.ForEachAsync(myItems, new ParallelOptions
{
CancellationToken = token
}
, async (myItem, ct) =>
{
i = Interlocked.Increment(ref i);
task1 = Task.Run(() => throw new SystemException($"Test exception in Task.Run {i}"), ct);
task2 = Task.Run(async () =>
{
await Task.Delay(1000, ct);
WriteToTextBox(myItem);
}, ct);
await Task.WhenAll(task1, task2);
});
}
catch (Exception ex)
{
WriteToTextBox(ex.Message);
}
Only one exception will be thrown in output:
item4
item2
item1
item5
item3
Test exception in Task.Run 5
Example download from
here