简体   繁体   English

如果有一些前台线程在控制台上打印内容,那么在主线程完成工作后,控制台窗口是否会关闭?

[英]Will the console window close after the main thread have done his work if there are some foreground threads that print something on the console?

Will the console window close after the main thread have done his work if there are some foreground threads that print something on the console? 如果有一些前台线程在控制台上打印内容,那么在主线程完成工作后,控制台窗口是否会关闭? So basically will text that is printed in other threads be shown on the console? 因此,基本上将在其他线程上打印的文本显示在控制台上吗?

Something like the following code: 类似于以下代码:

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

Depending on how you run the code (.NET Framework, .NET Core, or Mono) you should adjust the sleep time. 根据运行代码的方式(.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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM