当Asp.net终止后台线程? [英] When Asp.net terminates background threads?

查看:328
本文介绍了当Asp.net终止后台线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个项目,并有批量电子邮件发送的一部分,当用户点击该按钮时,他/她得到的谢谢你的邮件已发送立即作为响应和相同方法激发一个异步线程为好。

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);

当用户点击发送按钮,但是由于这是默认的后台线程我期待在页面响应结束后,该线程将被终止,但该线程排队并没有发生,因为当前线程时触发此主题也是一个后台线程工作线程,所以这意味着有一个未完成的前台线程(S)的(可能是MainThread或工作线程)的还活着,但我不知道他们什么时候完成,因为他们的完成时间会影响我的背景工作者线程;当最后一个前台线程结束,它会导致要终止的进程,所以做后台线程。

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.

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

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.

可否请你澄清的东西一点点?

Could you please clarify things a little bit ?

先谢谢了。

推荐答案

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

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).

手动创建线程为每个任务如果创建太多的线程还可以成为一个问题。

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.

这篇关于当Asp.net终止后台线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆