什么时候多线程控制台应用程序退出? [英] When does a multithreaded console application exit?
问题描述
class Program
{
public static void Main(String[] args)
{
var c = new C();
var thread = new Thread(new ThreadStart(c.F));
thread.Start();
Console.WriteLine("Exiting main, but the program won't quit yet...");
}
}
class C
{
public void F()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Waiting {0}", i);
Thread.Sleep(1000);
}
Console.WriteLine("Now the program will quit...");
}
}
什么是引擎盖下发生的事情与一个控制台应用程序?导致它在等待另一个线程退出(指向文档罚款)之前完成
What's going on under the hood with a console application that leads to it waiting for the other thread to finish before exiting (pointer to docs fine)?
的注:我知道这是一个基本的问题 - 我只是一直管理等待线程之前完成,并从未考虑过有一些基础设施,这样做是为了我... 的
推荐答案
一个过程结束时,所有前台线程终止
A process ends when all foreground threads terminate
从的 Thread.IsBackground 备注:
一个线程或者是一个背景螺纹或前台线程。后台线程是相同的前景线程,所不同的是后台线程不会阻止从终止的处理。一旦属于该进程的所有前台线程已经终止,公共语言运行时结束的过程。任何剩余的后台线程停止并没有完成。
A thread is either a background thread or a foreground thread. Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating. Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Any remaining background threads are stopped and do not complete.
这篇关于什么时候多线程控制台应用程序退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!