步骤:
1.准备一个测试程序,主窗口中通过按钮调起子窗口
2.子窗口添加启动线程的按钮,按钮事件代码见下方
3.禁用前台线程并启用后台线程代码,运行程序,显示子窗口,启动线程,关闭子窗口,控制台有打印输出,关闭主窗口,调试停止,打印停止
4.禁用后台线程并启用前台线程代码,运行程序,显示子窗口,启动线程,关闭子窗口,控制台有打印输出,关闭主窗口,调试仍然运行,打印还在继续
结论:
1.子窗口关闭时不影响前台及后台线程运行
2.前台线程阻止程序退出,后台线程不阻止
private void btn_EnableThreads_Click(object sender, EventArgs e) { //var thread1 = new Thread(() => //{ // for (int i = 0; i < 100; i++) // { // Console.WriteLine($"Background {i}"); // Thread.Sleep(500); // } //}); //thread1.IsBackground = true; //thread1.Start(); var thread2 = new Thread(() => { for (int i = 0; i < 100; i++) { Console.WriteLine($"No Background {i}"); Thread.Sleep(500); } }); thread2.Start(); }View Code
标签:窗口,Thread,线程,前台,关闭,后台 From: https://www.cnblogs.com/chixiner/p/18075804