我一直有这样的印象,即使在 ASP.NET 中,使用 ThreadPool 处理短期的后台任务(比如说非关键的)也被认为是最佳实践,但是后来我遇到了 这篇文章,它似乎给出了相反的建议——理由是你应该离开 ThreadPool 来处理与 ASP.NET 相关的请求。
以下是我到目前为止一直在做的小型异步任务:
ThreadPool.QueueUserWorkItem(s => PostLog(logEvent))
那篇文章建议明确地创建一个线程,类似于:
new Thread(() => PostLog(logEvent)){ IsBackground = true }.Start()
The first method has the advantage of being managed and bounded, but there's the potential (if the article is correct) that the background tasks are then vying for threads with ASP.NET request-handlers. The second method frees up the ThreadPool, but at the cost of being unbounded and thus potentially using up too many resources.
所以我的问题是,文章中的建议正确吗?
如果你的网站流量太大,你的线程池已经满了,那么是走出带外更好,还是一个完整的线程池意味着你已经达到了资源的极限,在这种情况下,你不应该尝试开始你自己的线程?
Clarification: I'm just asking in the scope of small non-critical asynchronous tasks (eg, remote logging), not expensive work items that would require a separate process (in these cases I agree you'll need a more robust solution).