ThreadPool.QueueUserWorkItem使用ASP.NetQueueUserWorkItem、ThreadPool、Net、ASP

2023-09-03 03:02:13 作者:温暖情系

在Asp.Net创建使用ThreadPool.QueueUserWorkItem一个巨大的PDF格式的报告(IAM),我的要求是报告已被异步创建的,我不想等待响应。我计划通过实现这一目标低于code

In Asp.Net for creating a huge pdf report iam using "ThreadPool.QueueUserWorkItem", My requirement is report has to be created asynchronously , and i do not want to wait for the Response. I plan to achieve it through below code

protected void Button1_Click(object sender, EventArgs e)
{
    ThreadPool.QueueUserWorkItem(report => CreateReport());
}

public void CreateReport()
{
    //This method will take 30 seconds to finish it work
}

我的问题是ThreadPool.QueueUserWorkItem会创建Asp.Net工作进程或某些系统线程一个新的线程。这是一个好的方法吗?,我可能并发用户访问的网页100。

My question is ThreadPool.QueueUserWorkItem will create a new thread from Asp.Net worker process or some system thread. Is this a good approach ?, I may have 100 of concurrent users accessing the web page.

推荐答案

QueueUserWorkItem()方法利用过程中的的线程池自动管理多工人线程。这些线程被分配一个任务,其中运行到完成,然后返回到线程池再使用。

The QueueUserWorkItem() method utilizes the process's ThreadPool which automatically manages a number of worker-threads. These threads are assigned a task, run them to completion, then are returned to the ThreadPool for reuse.

由于这是托管在ASP.NET线程池将属于ASP.NET进程。

Since this is hosted in ASP.NET the ThreadPool will belong to the ASP.NET process.

线程池是一个很好的候选人,这个类型的工作;作为旋转起来的专用线的另一种方法是相对昂贵的。但是,你应该考虑的线程池的下列限制,以及:

The ThreadPool is a very good candidate for this type of work; as the alternative of spinning up a dedicated thread is relatively expensive. However, you should consider the following limitations of the ThreadPool as well:

的线程池所使用的.NET的其他方面,并且提供的线程的数量有限。如果你过度使用它有你的任务将被阻塞,等待其他人来完成的可能性。这一点尤其在可扩展性方面关注的问题 - 但它不应该使用线程池,除非你有理由相信这将是一个瓶颈,劝阻你

The ThreadPool is used by other aspects of .NET, and provides a limited number of threads. If you overuse it there is the possibility your tasks will be blocked waiting for others to complete. This is especially a concern in terms of scalability--however it shouldn't dissuade you from using the ThreadPool unless you have reason to believe it will be a bottleneck.

线程池的任务,必须谨慎管理,以确保它们返回重用。被重用从后台线程未处理的异常或返回将基本上是泄漏线程和prevent它。在这些情况下的线程池可以有效地失去它的线程,并造成了严重的经济放缓过程中或暂停。

The ThreadPool tasks must be carefully managed to ensure they are returned for reuse. Unhandled exceptions or returns from a background thread will essentially "leak" that thread and prevent it from being reused. In these scenarios the ThreadPool may effectively lose it's threads and cause a serious slowdown or halt of the process.

您分配给线程池的任务应该是短暂的。如果你的处理密集型那么这是一个更好的主意,它提供一个专门的线程。

The tasks you assign to the ThreadPool should be short-lived. If your processing is intensive then it's a better idea to provide it with a dedicated thread.

所有这些主题涉及到简单的概念,该线程池是用于小任务,并为它的螺纹,以提供一节省成本的给消费code。通过被重用。您的情况听起来像一个合理的情况下,使用线程池 - 但你会想仔细$周围C $ C,并确保您运行现实的负载测试,以确定它是否是最好的办法

All these topics relate to the simple concept that the ThreadPool is intended for small tasks, and for it's threads to provide a cost-saving to the consuming code by being reused. Your scenario sounds like a reasonable case for using the ThreadPool--however you will want to carefully code around it, and ensure you run realistic load-tests to determine if it is the best approach.