Application_End和后台进程,正常退出ASP.Net应用程序 [英] Application_End and background processes, exiting ASP.Net application gracefully

查看:236
本文介绍了Application_End和后台进程,正常退出ASP.Net应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.Net应用程序,该应用程序使用ThreadPool.QueueUserWorkItem()触发了一些后台工作人员.在大多数情况下,后台线程将运行为零,但是有时一次最多可以执行5到6个后台线程.此外,大多数程序会在几秒钟内完成,但是它们可能会运行长达10分钟.

鉴于所有可能导致ASP.Net应用程序关闭的情况,我希望这些后台进程在应用程序需要关闭且处于处理过程中时尽可能地正常退出.

我认为创建一种方法来通知进程提前停止其工作并在接到有关该应用程序已关闭的呼叫的几秒钟内进行清理的信号时,我预计不会有太多的问题.尽管如果有人对此有具体建议,我当然会很感激.

我的两个主要问题是:

1)什么时候才是告诉后台工作人员整理事情的合适时间.在Application_End期间? Dispose()?也许是我不知道的第三个选择.

2)如果我在上述事件中等待进程完成然后再返回,那是不是没有立即关闭Application_End会比不很好地关闭后台作业导致应用程序更严重的问题.

void Application_End(object sender, EventArgs e) 
{
    //if this for whatever reason takes a non-trivial time to execute, what problems could I encounter? 
    SignalBackgroundJobsAndWaitForThemToShutDown();
}

谢谢!

解决方案

适当的时间在 Application_End

现在,在发出后台作业信号后,您必须在那一刻等待它们结束,然后再继续执行,就像您这样做一样.

进入我的行列,最好让等待时间超时,而不是永远等待,否则您的池可能有问题或关闭,那么您需要在最大等待时间上检查池设置以关闭池,其中设置的值大于您的等待值,或者只是将其禁用.

如果您有泳池花园(超过1个工作泳池),那么我的另一个想法就是每个泳池一次调用Application_End.

我在站点上使用相同的技术,以相同的方式向线程发出信号以相同的方式停止信号,我还记录了最终结果,并检查它们何时为最终结果,并且我强制例程停止执行并不要让它们运行很多时间.在回收池时,或者在打开app_offline.htm文件时,或者在关闭Web服务时,将调用Application_End.我个人已禁用回收站,不需要它们,并且仅在进行更新时打开app_offline.htm.那一刻,我等待池线程停止其工作.

I have an ASP.Net application which fires off some background workers using ThreadPool.QueueUserWorkItem(). Most of the time there will be zero background threads running, however it's possible that there could occationally be up to 5-6 executing at a time. Additionally, most will complete within a couple seconds, but it is possible that they could run for as long as 10 minutes.

Given all the different scenarios that could cause a shutdown of an ASP.Net application, I'd like to have these background processes exit gracefully if possible when the application needs to shutdown and they are in the middle of processing.

I don't anticipate having too much of an issue creating a way to signal the processes to stop their work early and clean up within a few seconds of getting the call that the application is being shut down. Although if anyone has specific advice on this I'd certainly appreciate it.

My two main questions are:

1) When would be the appropriate time to tell the background workers to wrap things up. During Application_End? Dispose()? Or perhaps a third option I'm not aware of.

2) If I wait during the above event for the processes to finish before returning, is there a point where not having Application_End return immediately could cause more serious issues for the application than not shutting down the background jobs nicely.

void Application_End(object sender, EventArgs e) 
{
    //if this for whatever reason takes a non-trivial time to execute, what problems could I encounter? 
    SignalBackgroundJobsAndWaitForThemToShutDown();
}

Thanks!

解决方案

The appropriate time is on the Application_End

Now after you signal your background jobs you must wait for them at that point to end, and then let continue, as its seems that you do.

Have in mine, that is good to place a time out on the wait, not wait for ever or else your pool may have problems, or shutdown, then you need to check your pool setting on the max wait to shutdown the pool, there set a value larger than your wait, or just disabled it.

Also have in mine that if you have pool garden, (more than 1 working pools) then the Application_End is called one time for each pool.

I use the same technique on my sites, the same way I signal my threads to stop the same way, I just also log the final end, and check when they are final end, and also I force my routines to stop and not let them run for many time. The Application_End is called on recycle the pool, or when you open the app_offline.htm file, or when you go to close the web service. Personally I have disable the recycles, I do not need them and I only open the app_offline.htm when I make updates. At that moment I wait my pool threads to stop their work.

这篇关于Application_End和后台进程,正常退出ASP.Net应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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