什么是在IIS管理后台线程的一些最佳做法? [英] What are some best practices for managing background threads in IIS?

查看:396
本文介绍了什么是在IIS管理后台线程的一些最佳做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个生成后台线程一个HttpModule。我使用的线程一样,在进程中运行的计划任务,这是非常方便的。

I have written an HttpModule that spawns a background thread. I'm using the thread like a Scheduled Task that runs in-process, which is really handy.

什么是保持这个线程的跟踪最佳做法?我从来没有这样做之前,我有点困惑的某些方面:

What are best practices for keeping track of this thread? I've never done this before, and I'm a little confused about some aspects of it:


  1. 我如何知道是否线程仍在运行?我看它做的工作,但有另一种方式来知道它是否还活着吗?我下载将procmon,但w3wp.exe的生成一个一大堆的线程,所以我不知道这是我的线程。我把它命名,但没有帮助。

  1. How do I know if the thread is still running? I see it do its job, but is there another way to know if it's still alive? I downloaded ProcMon, but w3wp.exe spawns a boatload of threads, so I had no idea which was my thread. I named it, but that didn't help.

我如何捕获如果死线程?是否有某种Dispose方法在那里我可以把它写,如果失败的事件日志什么的? A临终宣言什么?

How do I "catch" the thread if it dies? Is there some kind of Dispose method where I can have it write to the EventLog or something if it fails? A "dying declaration" or something?

我如何积极停止线程?如果我希望它停止运行这个后台进程,我怎么杀死它,而无需反弹IIS?

How do I actively stop the thread? If I want it to stop running this background process, how do I kill it without having to bounce IIS?

反正是有再次启动它,独立的HttpModule吗? (我猜的答案,这是没有...)

Is there anyway to start it again, independently of the HttpModule? (I'm guessing the answer to this is no...)

编辑:只是为了澄清,其用意是,我的线程永远不会消失。它运行一个函数,然后上床休息了几分钟,然后醒来并再次运行该功能。它不象它做一个任务,然后结束。

Just to clarify, the intention is that my thread never goes away. It runs a function, then goes to sleep for a couple minutes, then wakes up and runs the function again. It's not like it's doing one task then ending.

推荐答案

从我的经验,你可以得到这个工作足够好,但并不完美。我建议实行一个窗口服务的重复任务。根据什么任务做,Windows服务也许不会连得聊到Web应用程序或反之亦然,即G。如果两个具有相同的数据库。否则,你仍然可以使用电子邮件。 G。 WCF进行通信。

From my experience, you can get this to work "good enough", but not perfect. I would recommend to implement your repeating tasks in a windows service. Depending on what the tasks do, the Windows Service maybe wouldn't even have to talk to the web application or vice versa, e. g. if both work with the same database. Otherwise you could still use e. g. WCF for communication.

一大优势是:Windows服务将开始与操作系统,您可以轻松地配置,启动和使用控制面板停止它,你已经通过Windows事件日志内置的监控,可以更新后台服务和Web应用程序独立等。

The big advantage is: The windows service will start with the OS, you can easily configure, start and stop it using the control panel, you have built-in monitoring via Windows Event Log, you can update the background service and the web application independently etc.

如果这不是一个选项,例如G。因为你在一个共享的主机环境中,我提出以下建议:

If this is not an option, e. g. because you are in a shared hosting environment, I would recommend the following:


  1. 启动在的Application_Start(Global.asax中),您的后台线程,并存储在一个静态变量线程参考。

  2. 裹每呼吁用的try / catch你的后台线程的方法,因为自.NET 2.0,在后台线程每未处理的异常将关闭该应用程序。 (这将在下次请求重新启动,但它会减慢下一个请求,杀死所有当前会话和缓存,当然没有计时器将被激活,直到下一个请求。)

  3. 在每次请求(实施具有的HttpModule或在Global.asax中再次),在全局变量检查Thread实例(它仍然是!= null,也就是线程活跃,运行等)。如果没有,请致电重启code。使用在重启部分锁定,以确保线程不会在同一时间被创建的两倍。

即使是这样,你不能确定你的后台线程一直在运行,如果你不具备全天候正常交通。也请记住,在一个共享的主机环境中,它是很常见的关闭应用程序池,如果有几个小时没有任何活动。你可以尝试通过自己的网络上设置一个预定的任务在客户机上对应用程序做一个轻量的HTTP请求,每隔几分钟,以改善这一点,只是为了确保您的应用程序一直在运行。

Even then you can't be sure that your background thread is always running, if you don't have regular traffic around the clock. Also keep in mind that in a shared hosting environment it is very common to shut down application pools if there is no activity for a few hours. You could try to improve this by setting up a scheduled task on a client machine on your own network doing a light-weight HTTP request on your application every few minutes, just to ensure that your application is always running.

这篇关于什么是在IIS管理后台线程的一些最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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