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