ASP.NET multiple threads

五迷三道 提交于 2019-12-11 19:43:45

问题


I am looking to utilise running more than one one thread in my website.

In this instance, I have a task where I need to fire emails off to multiple users. I have to think about the fact that there could be 100's of emails sent at one time.

What I don't want, is for the end user to wait for these emails to be sent, it would take far too long. What I would like to do is send these emails on a separate thread, so that my current page can carry on processing the page.

The idea is that the user doesn't need to wait for these emails to be fired and completed, there is no message to advise the user that the emails have all successfully sent, its just something that will be done in the background.

The user just needs to be able to carry on oblivious with there usage of the system.

My Question is, what would be the best way to handle this problem.

Should I be looking at using a thread pool, or would it be better to use async methods?

Any advice would be appreciated.


回答1:


what would be the best way to handle this problem

Take the task right out your site completely.

Email notifications are a completely independent task which could (and should) be handled by a separate service. Make use of a reliable, persistent, messaging system e.g. MSMQ, and write a separate service to poll the queue and process the emails.




回答2:


If you spawn threads while handling a request, you should make sure they have all finished before returning a response. If you do leave threads running in the background, they could suddenly be aborted by an AppDomain recycle. More on that: http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx/

You should create a separate process (e.g., schedule a periodic task) responsible for sending out those emails.




回答3:


The best way to achieve this is to rely on an windows or external service.

If you cannot walk this way your best bet is to use Quartz.net

It'is not simple to use but it works quite well (with some caveats) in asp.net.

You can find an example with asp.net there http://blogs.planetcloud.co.uk/mygreatdiscovery/post/ASPNET-Scheduled-Tasks-with-QuartzNET.aspx



来源:https://stackoverflow.com/questions/20403354/asp-net-multiple-threads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!