programming4us
programming4us
ENTERPRISE

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

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

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
  •  
    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
    - Messages forwarded by Outlook rule go nowhere
    - Create and Deploy Windows 7 Image
    - How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
    - Creating and using an unencrypted cookie in ASP.NET
    - Directories
    - Poor Performance on Sharepoint 2010 Server
    - SBS 2008 ~ The e-mail alias already exists...
    - Public to Private IP - DNS Changes
    - Send Email from Winform application
    - How to create a .mdb file from ms sql server database.......
    programming4us programming4us
    programming4us
     
     
    programming4us