Using the Parallel class
This recipe shows you how to use the Parallel
class APIs. You will learn how to invoke methods in parallel, how to perform parallel loops, and tweak parallelization mechanics.
Getting ready
To work through this recipe, you will need Visual Studio 2015. There are no other prerequisites. The source code for this recipe can be found at BookSamples\Chapter7\Recipe1
.
How to do it...
To invoke methods in parallel, perform parallel loops, and tweak parallelization mechanics using the Parallel
class, perform the given steps:
Start Visual Studio 2015. Create a new C# console application project.
In the
Program.cs
file, add the followingusing
directives:using System; using System.Linq; using System.Threading; using System.Threading.Tasks; using static System.Console; using static System.Threading.Thread;
Add the following code snippet below the
Main
method:static string EmulateProcessing(string taskName) { Sleep(TimeSpan.FromMilliseconds( new Random(DateTime.Now.Millisecond...