Exception-handling wrappers for Task.ContinueWith()

The .NET 4 Task Parallel Library is great because you can specify continuations for new threads:

private Task<WebResponse> GetWebResponseAsync(string url)
{
    var webRequest = WebRequest.Create(url);
    return Task.Factory.FromAsync<WebResponse>(
        webRequest.BeginGetResponse,
        webRequest.EndGetResponse,
        null);
}

public void Run(string url)
{
    Task.Factory.StartNew(StartBusyIndicator)
        .ContinueWith(task => GetWebResponseAsync(url)).Unwrap()
        .ContinueWith(task => Console.WriteLine(task.Result.Headers))
        .ContinueWith(task => StopBusyIndicator());
}

Otherwise we’d have to handle the result of the async call either in callbacks or in event handlers—messy.

There are two caveats, though, when using Task.ContinueWith(). The first is that we have to sometimes use .Unwrap(). The second is that we have to handle exceptions in the next .ContinueWith(). Otherwise, our exceptions will just get swallowed.

So now:

public void Run(string url)
{
    Task.Factory.StartNew(StartBusyIndicator)
        .ContinueWith(task => GetWebResponseAsync(url)).Unwrap()
        .ContinueWith(task =>
        {
            if (task.IsFaulted) // handle errors
            else Console.WriteLine(task.Result.Headers);
        })
        .ContinueWith(task => StopBusyIndicator());
}

There goes our pretty code. Also, imagine the duplication if we do this for every .ContinueWith().

And so, inspired by Stephen Toub’s Processing Sequences of Asynchronous Operations with Tasks, I’ve written drop-in replacements for .ContinueWith() and .Unwrap() that will bubble up exceptions to a .Finally() extension method. Now our code can be clean again:

public void Run(string url)
{
    Task.Factory.StartNew(StartBusyIndicator)
        .Then(task => GetWebResponseAsync(url))
        .Then(task => Console.WriteLine(task.Result.Headers))
        .Finally(ExceptionHandler, StopBusyIndicator);
}

Do help yourself to the full source and example usage at github:gist.

28 May 2012 | .NET, C# | Comments

3 Responses to “Exception-handling wrappers for Task.ContinueWith()”

  1. 1 Rasmus Christensen 30 May 2012 @ 5:26 am

    Nice example. Looks like this could be the inspiration for an update on this sample. http://bypassion.dk/?p=214

  2. 2 Jannette Secky 13 July 2012 @ 7:54 pm

    We would like to thank you again for the lovely ideas you offered Jeremy when preparing her own post-graduate research as well as, most importantly, for providing many of the ideas in a blog post. If we had been aware of your website a year ago, i’d have been saved the pointless measures we were selecting. Thanks to you.

  3. 3 tomin 1 May 2014 @ 2:49 pm

    c# exception handling….

    http://csharp.net-informations.com/statements/csharp-exceptions.htm

    tomin

Comments:

  1.  
  2.  
  3.