gpt4 book ai didi

c# - 在 Web 应用程序中使用 ThreadPool.QueueUserWorkItem 有什么缺点吗?

转载 作者:行者123 更新时间:2023-11-30 21:43:52 25 4
gpt4 key购买 nike

我对这个主题做了一些研究,但找不到预期的答案。在我的应用程序中,我使用了

ThreadPool.QueueUserWorkItem

喜欢下面的方式。

ThreadPool.QueueUserWorkItem(o => CaseBll.SendEmailNotificationForCaseUpdate(currentCase, caseUpdate));

我在 asp.net mvc 中的应用程序和我已经处理了所有不需要在用户操作时执行的后台任务,以便更快地执行和快速用户响应。现在我想知道,当我们有更多的应用程序受众时,使用 ThreadPool.QueueUserWorkItem 是否有任何不好的一面。

最佳答案

不,你不应该使用它。我可以写出很多原因,但您应该阅读天才恕我直言的 Scott Hansleman 的这篇文章

http://www.hanselman.com/blog/ChecklistWhatNOTToDoInASPNET.aspx

在“可靠性和性能”下:

Fire-and-Forget Work - Avoid using ThreadPool.QueueUserWorkItem as your app pool could disappear at any time. Move this work outside or use WebBackgrounder if you must.

因此,按照建议,不要使用 ThreadPool.QueueUserWorkItem。一个很好的替代方法是:

https://www.asp.net/aspnet/overview/web-development-best-practices/what-not-to-do-in-aspnet-and-what-to-do-instead#fire

编辑:正如@Scott-Chamberlain 所提到的,这里有一个更好的链接: http://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx

关于c# - 在 Web 应用程序中使用 ThreadPool.QueueUserWorkItem 有什么缺点吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41266052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com