当Asp.net终止后台线程时?

时间:2022-02-27 21:01:37

I was working on a project and there is bulk e-mail sending part of it and when user clicks on the button, he/she gets the "Thanks the e-mails have been sent" immediately as a Response and the same method is firing an async thread as well.

我正在处理一个项目,并且有大量电子邮件发送它的一部分,当用户点击按钮时,他/她立即得到“谢谢电子邮件已发送”作为响应并且同样的方法正在触发一个异步线程。

ThreadPool.QueueUserWorkItem(SendEMail, _message);

This thread is queued when user clicks on the send button but since this is default Background Thread I was expecting when the page response ended, this thread would be terminated but it didn't happen because the current thread which fires this Thread was also a Background Thread and a Worker Thread, so which means there is an unfinished foreground thread(s) (Could be MainThread or Worker Threads) still alive but I don't know when they finish because their finish time will effect my background worker threads; When the last foreground thread ends, it causes the process to be terminated, so do background threads.

当用户点击发送按钮时,此线程排队,但由于这是默认的后台线程,我希望当页面响应结束时,此线程将被终止,但它没有发生,因为触发此线程的当前线程也是后台线程和工作线程,这意味着有一个未完成的前台线程(可能是MainThread或工作线程)仍然活着,但我不知道他们什么时候完成,因为他们的完成时间将影响我的后台工作线程;当最后一个前台线程结束时,它会导致进程终止,后台线程也是如此。

Should I be afraid of this or Asp.NET can handle it automatically and I am kinda confused because I've read a lot of things about it and now everything is mixed up.

我应该害怕这个,或者Asp.NET可以自动处理它,我有点困惑,因为我已经阅读了很多关于它的事情,现在一切都搞砸了。

Could you please clarify things a little bit ?

你能澄清一下吗?

Thanks in advance.

提前致谢。

3 个解决方案

#1


3  

Using the ThreadPool for long-running tasks will negatively influence the performance of your application (because ASP.NET also uses the ThreadPool to handle incoming requests).

将ThreadPool用于长时间运行的任务将对应用程序的性能产生负面影响(因为ASP.NET也使用ThreadPool来处理传入的请求)。

Creating threads manually for each task can also become a problem if too much threads are created.

如果创建了太多线程,则为每个任务手动创建线程也会成为问题。

One technique I've used in the past, is to create a custom ThreadPool when starting the application, and enqueuing tasks on that ThreadPool. A simple custom ThreadPool could consist of a single Thread and a Queue of tasks.

我过去使用的一种技术是在启动应用程序时创建自定义ThreadPool,并在该ThreadPool上排队任务。一个简单的自定义ThreadPool可以包含一个Thread和一个Queue of tasks。

#2


1  

When you call QueueUserWorkItem a new thread will be drawn from the thread pool if available and execute the callback function on this thread. If no threads are available, the function will block until a thread is freed. Once it finishes the execution of the method the thread will be suspended and returned to the pool for further reuse. Your only concern should be the fact that threads from the pool are also used to service requests, so if you perform lengthy tasks on them you could jeopardize the whole system request servicing capabilities because there's a limited number of threads in the pool and if all of them are busy performing lengthy operations future HTTP requests will be queued. As an alternative you could consider creating threads manually: new Thread(state => { }).Start();

当您调用QueueUserWorkItem时,将从线程池中提取新线程(如果可用)并在此线程上执行回调函数。如果没有可用的线程,该函数将阻塞,直到释放一个线程。一旦完成方法的执行,线程将被挂起并返回到池中以供进一步重用。您唯一关心的问题应该是来自池中的线程也用于处理请求,因此如果您对它们执行冗长的任务,则可能会危及整个系统请求服务功能,因为池中的线程数量有限且所有线程都是他们忙于执行冗长的操作,未来的HTTP请求将排队。作为替代方案,您可以考虑手动创建线程:new Thread(state => {})。Start();

#3


1  

You can use MSMQ for creating your email queue and have a single thread process the queue.

您可以使用MSMQ创建电子邮件队列,并使用单个线程处理队列。

The following post might be useful as it fits your problem domain. Although it does not use MSMQ but is a good post on processing scheduled tasks using ASP.net.

以下帖子可能有用,因为它适合您的问题域。虽然它不使用MSMQ,但是使用ASP.net处理预定任务是一个很好的帖子。

Simulate a Windows Service using ASP.NET to run scheduled jobs

使用ASP.NET模拟Windows服务以运行预定作业

#1


3  

Using the ThreadPool for long-running tasks will negatively influence the performance of your application (because ASP.NET also uses the ThreadPool to handle incoming requests).

将ThreadPool用于长时间运行的任务将对应用程序的性能产生负面影响(因为ASP.NET也使用ThreadPool来处理传入的请求)。

Creating threads manually for each task can also become a problem if too much threads are created.

如果创建了太多线程,则为每个任务手动创建线程也会成为问题。

One technique I've used in the past, is to create a custom ThreadPool when starting the application, and enqueuing tasks on that ThreadPool. A simple custom ThreadPool could consist of a single Thread and a Queue of tasks.

我过去使用的一种技术是在启动应用程序时创建自定义ThreadPool,并在该ThreadPool上排队任务。一个简单的自定义ThreadPool可以包含一个Thread和一个Queue of tasks。

#2


1  

When you call QueueUserWorkItem a new thread will be drawn from the thread pool if available and execute the callback function on this thread. If no threads are available, the function will block until a thread is freed. Once it finishes the execution of the method the thread will be suspended and returned to the pool for further reuse. Your only concern should be the fact that threads from the pool are also used to service requests, so if you perform lengthy tasks on them you could jeopardize the whole system request servicing capabilities because there's a limited number of threads in the pool and if all of them are busy performing lengthy operations future HTTP requests will be queued. As an alternative you could consider creating threads manually: new Thread(state => { }).Start();

当您调用QueueUserWorkItem时,将从线程池中提取新线程(如果可用)并在此线程上执行回调函数。如果没有可用的线程,该函数将阻塞,直到释放一个线程。一旦完成方法的执行,线程将被挂起并返回到池中以供进一步重用。您唯一关心的问题应该是来自池中的线程也用于处理请求,因此如果您对它们执行冗长的任务,则可能会危及整个系统请求服务功能,因为池中的线程数量有限且所有线程都是他们忙于执行冗长的操作,未来的HTTP请求将排队。作为替代方案,您可以考虑手动创建线程:new Thread(state => {})。Start();

#3


1  

You can use MSMQ for creating your email queue and have a single thread process the queue.

您可以使用MSMQ创建电子邮件队列,并使用单个线程处理队列。

The following post might be useful as it fits your problem domain. Although it does not use MSMQ but is a good post on processing scheduled tasks using ASP.net.

以下帖子可能有用,因为它适合您的问题域。虽然它不使用MSMQ,但是使用ASP.net处理预定任务是一个很好的帖子。

Simulate a Windows Service using ASP.NET to run scheduled jobs

使用ASP.NET模拟Windows服务以运行预定作业