WPF 4: Parallel processing of the Data using .NET 4 Parallel Extension Capabilities
Posted by: Mahesh Sabnis
in Category WPF
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:
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.
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:
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.
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 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 eBook 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 .NET Standard and the upcoming C# 8.0 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!