Parallel For and Foreach

Ive finally used the greatness known as the parallel.for and parallel.foreach. I just dont understand how I’ve lived so long without this greatness that has been bestowed upon us in the world known as .net. A parallel loop allows you to run each of the iterations of the loop simultaneously on separate threads at the same time. It uses what threads you have available to decide when it will run. Here are some code snippets below:


System.Threading.Tasks.Parallel.For(0, numOfIterations, Sub(i)
‘do whatever with your i var that you want to happen in parallel
End Sub)


System.Threading.Tasks.Parallel.ForEach(result.AssessmentDatasets(i).Tables(“Assessment”).AsEnumerable(), Sub(row)

”do whatever with your row var that you want to happen in parallel

End Sub)

If you look at the first var im just looping through some variable that will be incrementing for what ever the purpose of the for loop would be.

On the second one, the foreach, im looping through the rows of a datatable and processing each row in parallel. The AsEnumeralble is not a part of the datatable until you add a reference to System.Data.DataSetExtensions. once you add it, that function will become available.

This entry was posted in Uncategorized and tagged , , , , , , . Bookmark the permalink.

6 Responses to Parallel For and Foreach

  1. Discount Auto Parts says:

    Admiring the time and energy you put into your site and interesting insight you provide here. It’s very insiring to find a blog every once in a while that is not all the same out of date rehashed material. Fantastic work! I’ve bookmarked your site and I’m adding your site link to my Bookmarks now.

  2. Franklyn says:

    Major lover on this site, a large number of your blogposts have really helped me out. Looking forward to up-dates!

  3. Slyvia Rotenberry says:

    I have been reading out a few of your articles and i can state pretty good stuff. I will make sure to bookmark your blog.

  4. Deon Tyacke says:

    You really make it seem so easy with your presentation but I find this matter to be actually something that I think I would never understand. It seems too complex and very broad for me. I am looking forward for your next post, I’ll try to get the hang of it!

  5. Fajar says:

    Konbanwa I do trust all the ideas you’ve presented for your post. They’re very cninincovg and will certainly work. Nonetheless, the posts are too brief for beginners. May just you please prolong them a little from subsequent time? Thanks for the post .

  6. Cindi says:

    It’s hard to find your website in google. I found it on 16 spot,
    you should build quality backlinks , it will help you to increase
    traffic. I know how to help you, just search
    in google – k2 seo tricks

Leave a Reply to Deon Tyacke Cancel reply

Your email address will not be published. Required fields are marked *