2

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.

neildt
  • 5,101
  • 10
  • 56
  • 107
  • 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 ? – neildt Jan 13 '19 at 15:25
  • Ah i just skimmed you code and missed the loop. – Pretasoc Jan 13 '19 at 15:38

1 Answers1

3

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
}
Pretasoc
  • 1,116
  • 12
  • 22
  • 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 – Robert Koernke Jan 20 '21 at 20:40