如何确保应用程序结束时后台线程也结束?
我有一个带有后台线程的 asp.net 应用程序。该线程在应用程序启动时启动,并在应用程序结束时正常停止。我在共享主机上运行该网站。不幸的是,有时应用程序结束时不会触发 Application_End 事件。我认为线程无论如何都会被杀死,但事实并非如此。我目前有 4 个线程在后台运行。 3 个来自应用程序先前启动的时间,1 个来自当前应用程序会话。如何确保应用程序结束时线程关闭?有没有办法让线程检查应用程序是否已重置或已经被重置?或者有没有办法在应用程序启动时检查这些恶意线程并杀死它们?提前致谢。
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 website 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 ThreadPool.QueueUserWorkItem() 在后台执行代码,该代码将在 AppDomain 卸载时结束。根据标签,我假设您正在使用后台工作者,我建议您尝试这个。 (考虑添加示例代码以得出更好的答案。)
托管线程池:http:// /msdn.microsoft.com/en-us/library/0ka9477y.aspx
前台/后台线程:http://msdn.microsoft.com/en-us/library/h339syd0.aspx
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