I made up my mind to do couple of challenges starting today for the next 100 days which are #100DaysOfCode and #100DaysOfFitness. I will be publishing a blog every day on my progress on these challenges for the next 100 days.

For Coding part, I am planning on doing a Microsoft certification 70-483: Programming in C# in the coming months, so I will be preparing for the exam as a part of this challenge primarily and also some posts might be on algorithms and data structures. I have purchased Microsoft published book written by Rob Miles to prepare for the exam. As I read a new topic every day the code for those samples will be pushed to my GitHub, also I will use the post to describe the topic and share the code snippets which I have learnt. On the Fitness part it would be mainly hitting gym every day for the next 100 days, and I will cover the progress of that in the same post.

In this post I am going to talk about The Task Parallel Library, primarily on the three methods of Task.Parallel class which are Parallel.Invoke, Parallel.ForEach and Parallel.For. For the latter two I will also talk about managing the loop execution. A Task is an abstraction to unit of work to be performed. The work can be something like a method or code. The Task.Parallel class in this library provides us with three methods that can execute the tasks in parallel.

Parallel.Invoke

The Parallel.Invoke method is used to accept a number of action delegates and create a task for each one of them. An action delegate is an encapsulation of a method that accepts no input arguments and with no return type which means the return type should be void. If you are new to delegates I strongly encourage you to go through the MSDN documentation and read about them and also get familiarize with lambda syntax. The following code snippet illustrates how this method can be used.

As said the invoke method can start large number of tasks at once. You will not have control on order on how these tasks get started and assigned to processors. It returns when all the tasks have completed the run.

Parallel.ForEach

The ForEach method can be used to perform a parallel implementation of each task in foreach loop construction. It takes in two arguments one is IEnumerable collection and the second parameter provides the action to be performed  in each item in the list. Following code snippet illustrates how to use the ForEach method. Note that the tasks are not finished in the order they are started.

Parallel.For

The For method can be used to perform a parallel implementation of each task in for loop, it takes three arguments, first one is a starting loop counter, second argument is the length of the collection, the third parameter is a lambda expression which provides a variable that provides the counter value of each iteration. Below is a sample code which demonstrates the usage of For Method.

Managing Parallel.For and Parallel.Foreach

The For and ForEach methods return value of type ParallelLoopResult which can be used to determine whether the loop has successfully completed or not. Also The lambda expression the executes each iteration of the loop can be supplied with another variable of type ParallelLoopState which can be used to control the iteration process in other words to break or stop the execution. If stop method of LoopState is used then the ParallelLoopResult will be false along with null value of LowestBreakIteration. If break method of LoopState is used then the ParallelLoopResult will be false with non-null value of the LowestBreakIteration. Below code demonstrates usage of stop method of loopstate by terminating the loop when the counter is 200.

GitHub Repository for the code : https://github.com/santhoshrajr/MS-Exam70-483

I also went to gym this day, so I am doing good on Fitness part as well.

#Day1Of100DaysCode #Day1Of100DaysFitness

 

Last modified: March 11, 2019

Author

Comments

Write a Reply or Comment

Your email address will not be published.