WPF 4: Parallel processing of the Data using .NET 4 Parallel Extension Capabilities

Posted by: Mahesh Sabnis , on 2/15/2011, in Category WPF
Views: 70075
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.

If you are new to Parallel Processing in .NET 4.0, check out the article series Parallel Tasks in .NET 4.0

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:

wpf parallel layout xaml

The design of the window will be as shown below:

wpf parallel layout

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

WPF Employee Example

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

WPF EmployeeList

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:

employee observable

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

parallel processing employee

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.

To learn more, read the article Parallel.For Method in .NET 4.0 vs the C# For Loop

Step 6: Run the application. The following result will be displayed:

For vs Parallel.For

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.

The entire source code of this article can be downloaded over here

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Mahesh Sabnis is a DotNetCurry author and a Microsoft MVP having over two decades 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), and Front-end technologies like Angular and React. Follow him on twitter @maheshdotnet or connect with him on LinkedIn


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Mike on Friday, February 18, 2011 9:17 AM
I get an IndexOutOfRangeException in the Parallel.For statement.  Can you resolve this please?
Comment posted by Mike on Friday, February 18, 2011 11:06 AM
I get an IndexOutOfRangeException in the Parallel.For statement.  Can you resolve this please?
Comment posted by Mahesh Sabnis on Tuesday, February 22, 2011 12:03 AM
Hi Mike.
  Have you tried the same code which I have posted. If not then can you please post the code you are trying.
Thanks
Regards
Mahesh Sabnis
Comment posted by Glenn on Friday, February 25, 2011 9:52 AM
Thanks for the code. I was looking for something similar
Comment posted by Mahesh Sabnis on Saturday, February 26, 2011 1:54 AM
Hi Glenn,
  Thanks a lot.
Regards
Mahesh Sabnis
Comment posted by Mohit Bhardwaj on Tuesday, March 8, 2011 12:53 AM
But mahesh, is it possible for Parallel task library to do processing in the order of the list originally was.

I mean, if you look at the EmpNo for task Parallel Operation, you will see they are not sequential. Can sequencing and Parallelism be implemented together?

Consider executing this on some 80,000 records and end up getting result in arbitrary sequence or sort.

Thanks,
Mohit.