forum

Home / DeveloperSection / Forums / Creating a task inside a foreach loop

Creating a task inside a foreach loop

Anonymous User 1959 14-Aug-2014

I have a typical foreach loop that calls a method where the parameter is an element of the collection we're looping over; something like this:

foreach (byte x in SomeCollection)
{
   SomeMethod(x);
}

The problem is that SomeMethod takes a long time to run. I want to move the call into a new task so that the loop just creates the tasks and then the thread that called the loops just continues. How do I do this in a thread-safe way?

Thanks.


c# c# 
Updated on 14-Aug-2014
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By