- •Introduction to the Parallel Extensions Library
- •Objectives
- •System Requirements
- •Exercises
- •Next Step:
- •Figure 2
- •Figure 4
- •Parallel.Invoke(Sub() WalkTree(node.Left), Sub() WalkTree(node.Right))
- •Figure 6
- •Next Step:
- •Parallel.Invoke(delegate { WalkTree(node.Left); }, delegate { WalkTree(node.Right); });
- •Visual Basic
- •Figure 7
- •Figure 11
- •Next Step:
- •Figure 12
- •Next Step:
- •Figure 13
- •Figure 14
- •Figure 16
- •Next Step:
Figure 16
Output from parallelized LINQ query with extension methods
Task 3 – Using AsParallel() With Query Comprehension Syntax
In this task you will use the Parallel Extensions library and the AsParallel() method to create parallelized LINQ queries using the query comprehension syntax.
Replace the LINQ query in the Main() method with the following query comprehension syntax:
C#
static void Main(string[] args)
{
// Methods to call
Ex4Task3_PLINQComprehensionSyntax();
}
Visual Basic
Sub Main(ByVal args() As String)
...
' Methods to call
Ex4Task3_PLINQComprehensionSyntax()
...
End Sub
Add the Ex4Task3_PLINQComprehensionSyntax() method to Program.cs (C#) or Module1.vb (Visual Basic):
(Code Snippet – Intro to Parallel Extensions Lab - Ex4 Ex4Task3_PLINQComprehensionSyntax CSharp)
C#
private static void Ex4Task3_PLINQComprehensionSyntax()
{
var q = from e in employeeData.AsParallel()
where e.EmployeeID % 2 == 0
orderby e.EmployeeID
select PayrollServices.GetEmployeeInfo(e);
foreach (var e in q)
{
Console.WriteLine(e);
}
}
(Code Snippet – Intro to Parallel Extensions Lab - Ex4 Ex4Task3_PLINQComprehensionSyntax VB)
Visual Basic
Private Sub Ex4Task3_PLINQComprehensionSyntax()
Dim q = From e In employeeData.AsParallel()
Where e.EmployeeID Mod 2 = 0
Order By e.EmployeeID
Select PayrollServices.GetEmployeeInfo(e)
For Each e In q
Console.WriteLine(e)
Next e
End Sub
Build and run the application.
You should observe that although the LINQ syntax was changed, the data was processed in the same parallel manner as it was with the ParallelEnumerable extension methods.
Figure 17
Output from parallelized LINQ query using the query comprehension syntax
Next Step:
Summary
Summary
In this lab, you have worked with the Parallel Extensions library to understand its features to help you work with parallel tasks in a simple, controllable fashion. You’ve learned how to use Parallel Extensions classes like Parallel and Task to manage units of work. You’ve dealt with Parallel Extensions features like Wait(), WaitAll(), IsComplete(), and ContinueWith() to control the flow of your execution. You’ve also worked through examples of using PLINQ to deal with parallelizing queries.
This lab has given you a solid introduction to the Parallel Extensions library and its power and benefits. For more education we recommend you visit these locations:
The Parallel Extensions blog on MSDN: http://blogs.msdn.com/pfxteam/
The Parallel Computing Forms on MSDN: http://forums.microsoft.com/MSDN/default.aspx?ForumGroupID=551&SiteID=1
The Parallel Computing Developer Center: http://msdn.microsoft.com/en-us/concurrency/default.aspx.
