.NET应用程序是否在终止程序之前等待所有前台线程完成? [英] Does a .NET application wait for all foreground threads to finish before it terminates the program?

查看:88
本文介绍了.NET应用程序是否在终止程序之前等待所有前台线程完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉问这个相当愚蠢的问题,但是我制作了这个程序来测试在程序终止之前是否等待所有前台线程的完成.

Sorry for asking this rather silly question but I made this program to test whether or not all foreground threads are awaited for their completion before the program terminates.

但是在此程序中,只要我按任意键退出程序,即使在执行另一个前台线程的过程中,主线程也会终止,然后关闭应用程序.

But in this program, as soon as I hit any key to exit the program, the main thread terminates and then closes the application even while in the middle of executing the other foreground thread.

using System;
using System.Threading;

namespace ForegroundThreads
{
    // This is a program to test whether or not the application
    // will terminate when there is a pending foreground thread running.

    // In describing the difference between foreground and background threads, 
    // the documentation states that an application will terminate all
    // background threads when all foreground threads have finished execution.
    // This implies that the application will stall the main thread until
    // all foreground threads have completed execution. Let us see if this
    // is the case.

    // Quote:
    // A managed thread is either a background thread or a foreground thread. 
    // Background threads are identical to foreground threads with one exception: 
    // a background thread does not keep the managed execution environment running. 
    // Once all foreground threads have been stopped in a managed process (where the .exe file is a managed assembly), 
    // the system stops all background threads and shuts down. 
    // Source: https://msdn.microsoft.com/en-us/library/h339syd0%28v=vs.110%29.aspx

    class Program
    {
        static void Main(string[] args)
        {
            var t = new Thread(() => { 1000000.Times(() => { Console.WriteLine("Hello"); }); });
            t.Start();

            Console.WriteLine("Press any key to exit the main thread...");
            Console.ReadKey();
        }
    }

    public static class Extensions
    {
        public static void Times(this int numTimes, Action action)
        {
            for (int i = 0; i < numTimes; i++, action()) ;
        }
    }
}

运行此代码时我注意到的行为

在我的机器上,如果我将次数减少到较小的值(例如1000),则在主线程退出时,它将立即杀死所有前台线程.但是,如果我将价值提高了,比如说增加了一百万,那么系统将继续运行我创建的前台线程,而不管所有按键操作,直到完成打印一百万次为止.

On my machine, if I reduce the number of times to a smaller value, say, 1000, it immediately kills all foreground threads when the main thread exits. But if I make the value large, like one million, for instance, the system keeps running that foreground thread I created disregarding all keystrokes until it has finished printing one million times.

更新

GSerg链接到另一个询问相同问题的问题.但是,如果您仔细阅读该问题,则该问题的发布者实际上是在问:怎么回事?"

GSerg linked to another question that asks the same thing. However, if you read that question closely, the poster of that question is really asking, "What's going on?"

答案只是引用MSDN来解释所有等待的前台线程都在等待中.我的问题就是这个问题.

The answer simply quotes the MSDN explaining that all foreground threads are awaited for. My question contends the very thing.

我的问题是-为什么程序有时会等待前台线程完成而在其他时候却没有等待.因此,另一个问题中的答案对我没有帮助.

My question is -- why is it that the program waits for the foreground thread to finish sometimes and it doesn't at other times. Hence, that answer in that other question is of no help to me.

推荐答案

我不好,我的眼睛看不到20-20.该程序确实等待生成的前台线程完成执行.

My bad, my eyes were not seeing 20-20. The program does indeed wait for the spawned foreground thread to complete execution.

发生的事情是,当我通过减小Times方法的整数接收器的值来减少迭代次数时,即使程序实际上已经完成了所有 Hello 的打印, ,在我看来,该程序仍在忙于打印.那使我相信生成的线程仍在运行,并且当我按下键盘上的任何其他键时,它立即终止了该过程.

What was happening was that when I reduced the number of iterations by decreasing the value of the integer receiver of the Times method, even when the program actually had finished printing all the Hello's, it appeared to my eyes that the program was still busy printing. That led me to believe that the spawned thread is still running its course, and when I pressed any other key on my keyboard, it immediately terminated the process.

这篇关于.NET应用程序是否在终止程序之前等待所有前台线程完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆