当我重新启动在AppDomain前台线程没有举起应用 [英] Foreground thread not holding up application when I restart the AppDomain

查看:118
本文介绍了当我重新启动在AppDomain前台线程没有举起应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了MSDN上,一个前台线程和后台线程之间的区别是应用程序不能终止,直到所有的前台线程已经终止,但是它不会打扰等待后台线程。我决定尝试这只是为了获得更好的理解线程。

I read on MSDN that the difference between a foreground thread and a background thread is that an application can't terminate until all of its foreground threads have terminated, whereas it won't bother waiting for background threads. I decided to try this out just to get a better understanding of threads.

Thread t = new Thread(Work);  //Work() just has the thread sleep for a long time
t.IsBackground = false; //make it a foreground thread
t.Start();
while(true)
{
    if(Session["checker"] != null)
    {
        Session["checker"] = true;
        System.Diagnostics.Debug.Write("I'm here!");
    }
}

我用了一个会话变量知道,如果在AppDomain已经重新启动会议以来在得到一个AppDomain重启清除。

I used a session variable to know if the AppDomain has been restarted since sessions get cleared upon an AppDomain restart.

所以,当我保存的web.config文件,它应该触发的AppDomain重新启动,应该要求它等待我的长期运行的线程 T ,因为 T 在前台运行。然而,当我接触的web.config文件,直接进入清算我的会话[检查] 并打印出我在这里!,所以我知道我的应用程序没有等到我的主题。

So when I save the web.config file, it should trigger the AppDomain to restart, which should require it to wait for my long running thread t since t is running in the foreground. However when I touch the web.config file, goes straight into clearing my Session["checker"] and printing out "I'm here!", so I know that my application didn't wait for my thread.

我误解应该如何线程前台工作?应该不是的AppDomain重新启动等待我的线程执行完它去之前,并开始清理我的会话变量?

Am I misunderstanding how foreground threads are supposed to work? Shouldn't the AppDomain restart wait for my thread to finish executing before it goes and starts clearing my session variables?

感谢

推荐答案

请,注意单词应用程序,在声明中应用程序不能终止,直到所有的前台线程已终止。在回收的情况下,应用程序不会终止。在回收ASP.NET正在卸载旧应用程序域和加载新的。一切都在一个进程中完成的。过程没有结束。在卸载的AppDomain范围,所有线程(背景和前景)被杀害。
因此,ASP.NET不会等待前台线程回收过程中完成。
尝试创造在其Main方法前台线程简单的控制台应用程序。它会工作,直到线程终止。

Please, pay attention to the word "application" in statement "application can't terminate until all of its foreground threads have terminated". In case of recycling, application is not terminated. During recycling ASP.NET is unloading old AppDomain and loading new one. Everything is done inside single process. Process isn't terminated. In scope of AppDomain unloading, all threads (Background and Foreground) are killed. So, ASP.NET won't wait for foreground threads to complete during recycling. Try simple console application which creates a Foreground thread in its Main method. It will work until the thread is terminated.

这篇关于当我重新启动在AppDomain前台线程没有举起应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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