I have a class MyClass with a method MyMethod. For every MyClass instance in a list of MyClass instances i want to invoke MyMethod and have them run in a separate thread. I am using .NET 4.0 and the Parallel extensions.
Asked
Active
Viewed 448 times
2
-
do you really mean "separate thread", or do you just mean "as much in parallel as possible"? – Lasse V. Karlsen Jun 06 '10 at 16:03
1 Answers
5
Parallel.ForEach(MyClassList, myclass => myclass.MyMethod());
Note that this won't necessarily run every invocation in a separate thread; it'll use the available thread pool to try to achieve an appropriate level of parallelism.
It is, however, the equivalent of running all of those MyMethod
invocations in a big Parallel.Invoke
, which appears to be what you're looking for.

tzaman
- 46,925
- 11
- 90
- 115