gpt4 book ai didi

C# 等待所有线程在 Main() 中完成

转载 作者:行者123 更新时间:2023-11-30 21:03:21 25 4
gpt4 key购买 nike

我一直在编写一个多线程应用程序来解迷宫。我从 Program Class Main() 方法开始一个新线程,开始解决迷宫问题。如果发现 fork ,他将调用一个静态方法来创建一个新线程。我如何才能等待在后台运行的所有线程完成,以便显示完成消息等?

这是我正在尝试做的事情的骨架:

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();

有没有办法做到这一点?老实说,第一次以这种方式使用多线程,所以我为糟糕的代码道歉。我很乐意提供任何其他信息。提前致谢。

最佳答案

也许 Thread.Join()是你要找的吗?

您还可能会找到 this article有用。

关于C# 等待所有线程在 Main() 中完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12943489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com