When Asp.net terminates background threads?

前端 未结 3 1492
一向
一向 2021-01-27 10:42

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

相关标签:
3条回答
  • 2021-01-27 11:27

    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.

    0 讨论(0)
  • 2021-01-27 11:31

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

    0 讨论(0)
  • 2021-01-27 11:31

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

    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.

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

    0 讨论(0)
提交回复
热议问题