Abstract: In this article, I have demonstrated how to use Parallel Processing programming in WPF 4.0 for DataBinding and Data processing, using .NET 4 Parallel Extension Capabilities.

NET 4.0 adds a new set of API's called Parallel Extensions, to simplify the process of adding parallelism and concurrency to applications. This allows you to effectively utilize Multi-Core systems which are commonly available now days. Parallel Extensions are composed of PLINQ and Task Parallel Library (TPL). With .NET 4.0, we have been provided a new namespace ‘System.Threading.Task’ in comprising of TPL classes. This namespace defines classes for performing Parallel looping operations, asynchronous task operations etc. In this article, I have demonstrated how to use Parallel programming capabilities in WPF 4.0 for DataBinding and Data processing.

Step 1: Open VS 2010 and create a new WPF 4.0 application. Name it as ‘WPF40_Parallel_Procesing’.

Step 2: Open MainWindow.xaml and write the following markup in it:

The design of the window will be as shown below:

Step 3: Open MainWindow.xaml.cs and add the the Employee class to it:

Now add an EmployeeList class and a ProcessTax class as shown below

The Employee class defines Employee properties and the EmployeeList class defines the Employee collection. The ProcessTax class contains a method to calculate Tax applicable for each employee.

Step 4: In the MainWindow.xaml.xs class, define the following objects:

Step 5: In the window_loaded event, write the following code:

The above code demonstrates both the mechanism for processing EmployeeList. The first uses a standard ‘for’ loop. This loop process EmployeeList sequentially based upon the index. The issue in this approach is that unless the previous Employee is not processed, the processing on next will not occur. This increases the processing time.

In the second case, ‘Parallel’ class and its ‘For’ method is used. This processes the records in parallel and thus reduces the time required for processing the entire collection.

Note the time taken in Parallel data processing is less (almost half). Since ours is just a sample application, the difference in processing will be not be very noticeable. However this approach is significant when you are processing tasks that take longer time.

Mahesh Sabnis is a DotNetCurry author and Microsoft MVP having over 17 years of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions). Follow him on twitter @maheshdotnet

Feedback - Leave us some adulation, criticism and everything in between!