所有线程完成执行后如何显示消息框?

pri*_*yam 1 .net c# data-structures c#-4.0

想要在完成工作时使用线程分解非常繁重的函数并显示消息框.

以下是类似的代码:

for (int index = 0; index < tests.Count; index/=3)
{
    System.Threading.Thread t =
        new System.Threading.Thread(
            () => SomeFunction(tests.GetRange(start, 3)));
    t.Start();
}
Run Code Online (Sandbox Code Playgroud)

一旦所有这些线程完成执行并完成整个工作,就要显示一个消息框.我该怎么做?加入将不起作用,因为它会导致不使用线程的同时.

Sri*_*vel 7

如果你在.Net4.0 +中,就扔掉旧学校的话题.使用Task paralell库.

var uiContext = TaskScheduler.FromCurrentSynchronizationContext();
var tasks = ...;//Start tasks
Task.Factory.ContinueWhenAll(tasks, antecedents =>
{
   //Show messagebox here 
},CancellationToken.None,TaskContinuationOptions.None,uiContext);
Run Code Online (Sandbox Code Playgroud)

等待异步使用ContinueWhenAll并在UIThread中执行回调FromCurrentSynchronizationContext.