开发者

How to ensure background thread ends when application ends?

I have an asp.net application with a background thread. The thread starts when the application starts and it is gracefully stopped when the application ends. I am running the webs开发者_高级运维ite on a shared host. Unfortunately sometimes the application does not trigger the Application_End event when it ends. I would think that the threads would be killed anyway, but that's not the case. I currently have 4 threads running in the background. Three from previous times the application started and 1 from the current application session. How can I ensure that the threads are shutdown when the application ends? Is there a way for the threads to check if the application was reset or had been reset? Or is there a way to check for these rogue threads at application startup and kill them? Thanks in advance.


Use the ThreadPool.QueueUserWorkItem() to execute your code in the background, which will be ended when the AppDomain unloads. I'm assuming that you are using a backgroundworker, according to the tags, and I would suggest trying this instead. (Consider adding sample code to draw out better answers.)

Managed ThreadPool: http://msdn.microsoft.com/en-us/library/0ka9477y.aspx

Foreground/Background Threads: http://msdn.microsoft.com/en-us/library/h339syd0.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜