如果有一些前台线程在控制台上打印某些东西线程,那么在主线程完成其工作后控制台窗口是否会关闭?所以基本上将在控制台上显示在其他线程中打印的文本?
类似下面的代码:
public static void Main(string[] args)
{
Console.WriteLine("string");
var threads = new Thread[5];
for (int i = 0; i < threads.Length; ++i)
{
threads[i] = new Thread(() => Console.WriteLine("smth"));
threads[i].Start();
}
}
根据您运行代码的方式(.NET Framework,.NET Core或Mono),您应该调整睡眠时间。
public static void Main(string[] args)
{
Console.WriteLine("string");
var threads = new Thread[5];
for (int i = 0; i < threads.Length; ++i)
{
threads[i] = new Thread(() => {
Thread.Sleep(1000);
Console.WriteLine("smth");
}) { IsBackground = true };
threads[i].Start();
}
}