[英]C# wait for all threads to finish in Main()
I've been coding a Multithreading Application to solve mazes. 我一直在编写多线程应用程序以解决迷宫问题。 I start a new thread from my Program Class
, Main()
method, to begin solving the maze. 我从Program Class
Main()
方法开始一个新线程,开始解决迷宫问题。 He will then call a static method to create a new thread if he finds a bifurcation. 如果发现分支,他将调用一个静态方法来创建新线程。 How can I wait for all the threads running in the background to finish, in order to, for example display a message of completion? 我如何等待在后台运行的所有线程完成,以便例如显示完成消息?
This is an eskeleton of what I'm trying to do: 这是我要做的事情的基本内容:
var explorer = new Explorer(/*Some arguments*/);
var thread = new Thread(explorer.Explore) {Name = "Thread 0"};
thread.Start();
//Thread_0_and_Threads_he_generates_through_static_class.Join()
Console.WriteLine("I'm done bro.");
Console.ReadKey();
Is there a way to do this? 有没有办法做到这一点? Honestly, first time working with Multithreading in this way, so I apologize for the poor code. 老实说,第一次使用这种方式使用多线程,因此我为糟糕的代码表示歉意。 Any other information I'll be more than happy to provide. 我将很乐意提供任何其他信息。 Thanks in advance. 提前致谢。
Perhaps Thread.Join() is what you're looking for? 也许您正在寻找Thread.Join() ?
you also might find this article usefull. 您可能还会觉得这篇文章有用。
Multi-threading is a really delighfull subject. 多线程是一个非常完善的主题。
Since you are taking your first steps on threading, I would suggest you to carefully refer to Microsoft's Thread Synchronization excelent begginers article. 由于您正在迈出线程的第一步,因此建议您仔细参考Microsoft的《 线程同步入门》一文。
Happy theading! 节日快乐!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.