【发布时间】:2012-07-12 00:15:47
【问题描述】:
可能重复:
C# Spawn Multiple Threads for work then wait until all finished
我有两个方法调用,我想使用两个线程进行调用。然后我希望他们等到方法执行完成后再继续。我的示例解决方案如下所示。
public static void Main()
{
Console.WriteLine("Main thread starting.");
String[] strThreads = new String[] { "one", "two" };
String ctemp = string.Empty;
foreach (String c in strThreads)
{
ctemp = c;
Thread thread = new Thread(delegate() { MethodCall(ctemp); });
thread.Start();
thread.Join();
}
Console.WriteLine("Main thread ending.");
Console.Read();
}
public static void MethodCalls(string number)
{
Console.WriteLine("Method call " + number);
}
这样可以吗?还是有其他更好的方法来做同样的事情?
【问题讨论】:
-
扔掉你的'thread.Join();'循环外的语句。
标签: c# .net multithreading