3

I have the following code which is firing off a number of async Task.

 List<TimeoutException> TimeoutExceptions = new List<TimeoutException>(); List<TaskCanceledException> TaskCanceledExceptions = new List<TaskCanceledException>(); List<Exception> Exceptions = new List<Exception>(); List<AggregateException> AggregateExceptions = new List<AggregateException>(); List<Models.Channel.IChannel> channels = new List<Models.Channel.IChannel>(); channels.Add(new Models.Channel.DummyChannelName()); var tasks = new List<Task>(); foreach (Models.Channel.IChannel channel in channels) { try { var cts = new CancellationTokenSource(); cts.CancelAfter(channel.TimeOut); tasks.Add(Task.Run(() => { channel.Data = channel.RequestOffers(new Models.Request.AvailabilityRequest()).Result; if (cts.Token.IsCancellationRequested) cts.Token.ThrowIfCancellationRequested(); }, cts.Token)); } catch (TimeoutException t) { TimeoutExceptions.Add(t); } catch (TaskCanceledException tc) { TaskCanceledExceptions.Add(tc); } catch (AggregateException ae) { AggregateExceptions.Add(ae); } catch(Exception ex) { Exceptions.Add(ex); } } Task.WaitAll(tasks.ToArray(), TimeSpan.FromSeconds(5)); 

The problem I have is that if a task is cancelled because of a timeout I'm getting the following exception

<ExceptionMessage>One or more errors occurred.</ExceptionMessage> <ExceptionType>System.AggregateException</ExceptionType> 

Is it just a simple case that I need a Try Catch around Task.WaitAll, or should my code be structured differently.

2
  • Is that a problem if I have Task.WaitAll called multiple times, as it will be within my foreach loop ? Or are you saying the Task.WaitAll should have it's own try/catch block ? Commented Jan 13, 2019 at 15:25
  • Ah i just skimmed you code and missed the loop. Commented Jan 13, 2019 at 15:38

1 Answer 1

4

If an exception occurs inside a task the exception is raised on the calling thread when you Wait for a task. If a task is sucessfully created all exceptions that occur inside the task are wrapped in an AggregateException and thrown on the wait.

For you example this means you can remove the try/catch block within your loop and use it to wrap the Task.WaitAll(...) after the loop.

var tasks = new List<Task>(); foreach (Models.Channel.IChannel channel in channels) { Task myTask = Task.Run(...); // create your task here tasks.Add(myTask); } try { Task.WaitAll(tasks.ToArray(), TimeSpan.FromSeconds(5)); } catch { // Insert Exception handling logic } 
Sign up to request clarification or add additional context in comments.

1 Comment

I have a question... if you plop the WaitAll in a try-catch block, and you have 5-tasks. One of five throws an exception. Will it pop out, and not wait for the other 4 (good) tasks? Are there other considerations in putting a WaitAll in a trycatch block

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.