如何确保应用程序结束时后台线程也结束?

发布于 2024-09-13 14:26:01 字数 267 浏览 9 评论 0原文

我有一个带有后台线程的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娜些时光,永不杰束 2024-09-20 14:26:01

使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文