C# 如何在继续之前等待线程完成?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-07-31 16:44:58  来源:igfitidea点击:

How to wait for thread complete before continuing?

提问by Brian Lyttle

I have some code for starting a thread on the .NET CF 2.0:

我有一些用于在 .NET CF 2.0 上启动线程的代码:

ThreadStart tStart = new ThreadStart(MyMethod);
Thread t = new Thread(tStart);
t.Start();

If I call this inside a loop the items completely out of order. How do introduce a wait after t.Start(), so that the work on the thread completes before the code continues? Will BeginInvoke/EndInvoke be a better option for this than manually creating threads?

如果我在循环中调用它,则项目完全无序。如何在 之后引入等待t.Start(),以便在代码继续之前完成线程上的工作?与手动创建线程相比,BeginInvoke/EndInvoke 会是更好的选择吗?

采纳答案by Dominic Cooney

How much order do you need to impose on the threads? If you just need all of the work started in the loop to finish before the code continues, but you don't care about the order the work within the loop finishes, then calling Join is the answer. To add more detail to Kevin Kenny's answer,you should call Join outsidethe loop. This means you will need a collection to hold references to the threads you started:

您需要对线程施加多少顺序?如果您只需要在代码继续之前完成循环中开始的所有工作,但您不关心循环内工作完成的顺序,那么调用 Join 就是答案。要为Kevin Kenny 的回答添加更多详细信息您应该在循环调用 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();
}

In contrast, if you called Join inside the loop, the result would basically be the same as not using threads at all. Each iteration of the loop body would create and start a thread but then immediately Join it and wait for it to finish.

相比之下,如果在循环内调用 Join,结果基本上与根本不使用线程相同。循环体的每次迭代都会创建并启动一个线程,然后立即加入它并等待它完成。

If the individual threads produce some result (write a message in a log, for example) then the messages may still appear out of order because there's no coordination between the threads. It is possible to get the threads to output their results in order by coordinating them with a Monitor.

如果单个线程产生了一些结果(例如在日志中写入一条消息),那么这些消息可能仍会出现乱序,因为线程之间没有协调。通过将线程与监视器进行协调,可以让线程按顺序输出其结果。

回答by Kev

If I'm reading this properly, you're starting work items on bunch of threads but you're concerned that the threads are completing out of order, and, would prefer to wait until each thread completes in the order they were started ?

如果我正确阅读本文,您将在一堆线程上启动工作项,但您担心线程无序完成,并且希望等到每个线程按照它们启动的顺序完成?

If this is the case then I'm not sure why you're creating a thread per work item. Maybe you could expand?

如果是这种情况,那么我不确定您为什么要为每个工作项创建一个线程。也许你可以扩展?

If you did need to wait for a thread to complete the you could just do:

如果您确实需要等待线程完成,则可以执行以下操作:

t.Join();

回答by ollifant

Another way of waiting for a thread to finish is using an AutoResetEvent.

等待线程完成的另一种方法是使用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();
}