简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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