0

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.

8
  • 2
    Ideally, your page shouldn't be responsible for sending emails at all. Commented Dec 5, 2013 at 15:03
  • It's a Content management page, and the code that sends the emails lives in a service, the service is called by the page. Commented Dec 5, 2013 at 15:05
  • Apart from what @DanielMann said, you may want to look at sendasync. Commented Dec 5, 2013 at 15:05
  • async methods will not help at all. You still need to wait for all the requests to return. You'd probably be better of kicking of a separate background process Commented Dec 5, 2013 at 15:06
  • 1
    So if you already have a service handling sending the emails, why not have the service handle sending all of the emails, rather than calling the service hundreds of times? Commented Dec 5, 2013 at 15:06

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I agree! We had the same idea. :)
3

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.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.