解决方案
调用Join
//Start all of the threads.
List<Thread> startedThreads = new List<Thread>();
foreach (...) {
Thread thread = new Thread(new ThreadStart(MyMethod));
thread.Start();
startedThreads.Add(thread);
}
//Wait for all of the threads to finish.
foreach (Thread thread in startedThreads) {
thread.Join();
}
等待线程完成的另一种方法是使用AutoResetEvent。
private readonly AutoResetEvent mWaitForThread = new AutoResetEvent(false);
private void Blah()
{
ThreadStart tStart = new ThreadStart(MyMethod);
Thread t = new Thread(tStart);
t.Start();
//... (any other things)
mWaitForThread.WaitOne();
}
private void MyMethod()
{
//... (execute any other action)
mWaitForThread.Set();
}
日期:2020-03-25 13:13:56 来源:oir作者:oir
