Parallel.Invoke the same method for a list of objects
I have a class MyClass with a method MyMethod. For ev开发者_JAVA百科ery 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.
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.
精彩评论