Book Image

.NET 4.5 Parallel Extensions Cookbook

By : Bryan Freeman
Book Image

.NET 4.5 Parallel Extensions Cookbook

By: Bryan Freeman

Overview of this book

<p>.NET parallel extensions brings the power of parallel and asynchronous programming to a much wider developer audience than ever before. This book will give a developer with no multithreaded development experience the ability to write highly scalable parallel applications that take advantage of modern multicore processors.If you are an experienced .NET developer who wants to put parallel extensions to work in your applications, this book is for you.</p> <p>".NET 4.5 Parallel Extensions Cookbook" is a practical, hands-on guide that provides you with a number of clear step-by-step recipes that will introduce parallelism into your applications and take advantage of modern multicore processors. This book is a crash course in using the extensions, with theory and concepts kept to a minimum.</p> <p>".NET 4.5 Parallel Extensions Cookbook" offers a wide-ranging presentation of parallel development concepts, and provides a working knowledge of key technologies that are important to developers who want to take advantage of multi-core architectures.</p> <p>You will learn how to compose a series of producer/consumer tasks into a pipeline that can process data elements received from a real-time event stream. You will also learn how to connect the stages of pipelines together using the concurrent collections. You will learn everything you need to know to transform the multicore power found in modern processors into application performance and scalability.</p>
Table of Contents (16 chapters)
.NET 4.5 Parallel Extensions Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Handling task exceptions with AggregateException.Handle


In this recipe, we will look at another way to handle System.AggregateException, by using the AggregateException.Handle method. The Handler method invokes a handler function for each exception wrapped in AggregateException.

Getting ready…

For this recipe, we need to turn off the Visual Studio 2012 Exception Assistant. The Exception Assistant appears whenever a runtime exception is thrown and intercepts the exception before it gets to our handler.

  1. To turn off the Exception Assistant, go to the Debug menu and select Exceptions.

  2. Uncheck the user-unhandled checkbox next to Common Language Runtime Exceptions.

How to do it…

Let's take a look at how we can use AggregateException.Handle to provide an alternate method to handling exceptions in a parallel application.

  1. For this recipe, we will return to our WordCount6 project, and modify it to handle our exceptions in a different way. Start Visual Studio 2012 and open the WordCount6 project.

  2. The first step is to define our handler function that will be invoked when we call AggregateException.Handle. Following the Main method of your Program class, add a new private static handler method that returns a bool. It should look as the following code snippet:

    private static bool HandleWebExceptions(Exception ex)
    {
        if (ex is WebException)
        {
          Console.WriteLine(("Caught WebException: {0}", ex.Message);
          return true;
        }
        else
        {
          Console.WriteLine("Caught exception: {0}", ex.Message);
          return false;
        }
    }
  3. The only other step here is to replace the body of your catch block with a call to System.AggregateException.Handle, passing in the HandleWebExceptions predicate. The updated try/catch block should look as follows:

    try
    {
        task1.Wait();   
        if (!task1.IsFaulted)
        {
          Console.WriteLine("Task complete. Origin of Species word count: {0}",task1.Result);
        }
    }
    catch (AggregateException aggEx)
    {
        aggEx.Handle(HandleWebExceptions);                
    }
  4. Those are the only modifications necessary. In Visual Studio 2012, press F5 to run the project. You should see output as shown in the following screenshot:

How it works…

AggregateException.Handle() takes a predicate that you supply, and the predicate will be invoked once for every exception wrapped in System.AggregateException.

The predicate itself just needs to contain the logic to handle the various exception types that you expect, and to return true or false to indicate whether the exception was handled.

If any of the exceptions went unhandled, they will be wrapped in a new System.AggregateException and thrown.