Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个带有方法 MyMethod 的类 MyClass。对于 MyClass 实例列表中的每个 MyClass 实例,我想调用 MyMethod 并让它们在单独的线程中运行。我正在使用 .NET 4.0 和并行扩展。
Parallel.ForEach(MyClassList, myclass => myclass.MyMethod());
请注意,这不一定会在单独的线程中运行每个调用;它将使用可用的线程池来尝试实现适当的并行度。 然而,这相当于MyMethod在一个 big 中运行所有这些调用Parallel.Invoke,这似乎是您正在寻找的。
MyMethod
Parallel.Invoke