1

I should use Parallel.ForEach loop, and inside there should be call to method.

Parallel.ForEach(myList, item => {  
   DoSomethingWithItem(item);
 }
);

Should that method be Task or whatever?

private Task DoSomethingWithItem(MyClass item);

Also, this method shouldn't have return type, but I have warning "not all code paths return a value". Tried to put void keyword but seems it doesn't go in same context with Task keyword.

1 Answer 1

3

No

Do not make it more complicated than it has to be.

Parallel.ForEach(myList, item => {  
   DoSomethingWithItem(item);
 }
);

is perfectly fine for void DoSomethingWithItem(MyClass item).

Making it "more" async is overkill at best, counterproductive in the worst case.

Sign up to request clarification or add additional context in comments.

2 Comments

Would it be problem if DoSomethingWithItem has few tasks that should perform? public void DoSomethingWithItem(MyClass item) { var tasks = new Task[3]; task[0] = DoSomething1(item); task[1] = DoSomething2(item); task[2] = DoSomething3(item); }
The syntax of running multiple tasks in parallel is not correct, but generally speaking, yes, if your method wants to start multiple tasks and wait for all, that's fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.