ENTERPRISE

Microsoft Visual Studio 2010 : Data Parallelism - Unrolling Sequential Loops into Parallel Tasks (part 4) - Handling Exceptions

10/18/2013 7:05:56 PM

4. Handling Exceptions

You can raise an exception in a parallel loop, but you should consider several factors when the need to do this occurs:

  • Parallel tasks already running are allowed to run until completion. This means that tasks might continue to run after the unhandled exception occurs.

  • In most circumstances, iterations not started are not allowed to run after the exception.

  • Long-running tasks should check the ParallelLoopState.IsExceptional property for pending exceptions. The property returns true when an exception is pending. If a pending exception is discovered, the task should end at the earliest convenient moment.

  • Because tasks are running in parallel, the possibility exists that more than one exception might be raised. For that reason, the method throws an AggregateException. You can use the AggregateException.InnerExceptions property to enumerate the underlying exceptions.

  • Unhandled exceptions within a parallel loop are caught on the joining thread. If the parallel call was not made within the scope of a try/catch block, the exception might cause the application to fail.

  • An unhandled exception takes precedence over a ParallelLoopState.Break or ParallelLoopState.Stop.

The following code demonstrates the proper technique to handle an unhanded exception that occurs within a parallel loop. The code purposely raises an unhandled exception in the fourth task. Remember, the Parallel.For statement must be within the scope of a try block so that if an unhandled exception is raised, execution gets transferred to the joining thread—and ultimately to the catch filter, where you actually catch an AggregateException exception. Internally, the catch block enumerates the AggregateException.InnerExceptions collection and displays the unhandled exceptions.

try
{
Parallel.For(0, 6, (index) =>
{
Console.WriteLine("Task {0} started.", index);
if (index == 4)
{
throw new Exception();
}
DoSomething();
Console.WriteLine("Task {0} ended.", index);
});
}
catch (AggregateException ax)
{
Console.WriteLine("\nError List: \n");
foreach(var error in ax.InnerExceptions)
{
Console.WriteLine(error.Message);
}
}

Here’s the output from the preceding code.

Handling Exceptions

5. Dealing with Dependencies

You should strive for independent loop iterations, because that provides the maximum parallel performance, allowing you to use embarrassingly parallel loops. However, not every loop has perfectly independent iterations. This is particularly true when you are porting sequential code to parallel code, where the original developer was working under entirely different assumptions and constraints. Dependencies, if handled incorrectly, can cause unreliable results and, in some circumstances, application crashes. Dependencies are typically resolved through some degree or synchronization, which can adversely affect performance. However, correctness is sometimes more important than performance.

Other  
  •  Programming Windows Services with Microsoft Visual Basic 2008 : Implementing the Worker Class, Creating the FileWorkerOptions Class
  •  Programming Windows Services with Microsoft Visual Basic 2008 : Extending the Threading Model
  •  Programming Windows Services with Microsoft Visual Basic 2008 : Writing a New Thread Method, Monitoring with Multiple Threads
  •  The HP Virtual Server Environment : Example nPartition Management Scenario (part 4) - Rebooting and Booting nPartitions
  •  The HP Virtual Server Environment : Example nPartition Management Scenario (part 3) - Creating a new nPartition
  •  The HP Virtual Server Environment : Example nPartition Management Scenario (part 2) - Viewing the Complex after Installing Hardware, Extending the Existing nPartition
  •  The HP Virtual Server Environment : Example nPartition Management Scenario (part 1) - Viewing the Configuration of an nPartition Complex
  •  The HP Virtual Server Environment : nPartition Management Paradigms (part 2) - Remote Management via an nPartition Paradigm, Remote Management via the MP Paradigm
  •  The HP Virtual Server Environment : nPartition Management Paradigms (part 1) - Local nPartition Management Paradigm
  •  The HP Virtual Server Environment : nPartition Servers - Data Maintained by the Management Processor
  •  
    Video
    Video tutorials
    - How To Install Windows 8

    - How To Install Windows Server 2012

    - How To Install Windows Server 2012 On VirtualBox

    - How To Disable Windows 8 Metro UI

    - How To Install Windows Store Apps From Windows 8 Classic Desktop

    - How To Disable Windows Update in Windows 8

    - How To Disable Windows 8 Metro UI

    - How To Add Widgets To Windows 8 Lock Screen

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010
    programming4us programming4us
    Top 10
    Free Mobile And Desktop Apps For Accessing Restricted Websites
    MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
    TOYOTA CAMRY 2; 2.5 : Camry now more comely
    KIA SORENTO 2.2CRDi : Fuel-sipping slugger
    How To Setup, Password Protect & Encrypt Wireless Internet Connection
    Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
    Backup & Restore Game Progress From Any Game With SaveGameProgress
    Generate A Facebook Timeline Cover Using A Free App
    New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    Popular Tags
    Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Exchange Server Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe Photoshop CorelDRAW X5 CorelDraw 10 windows Phone 7 windows Phone 8 Iphone