• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 566
  • Last Modified:

ASP.net async emails

Hello

Can anyone tell me what the performance implications are of sending emails asynchronously in asp.net when there are hundreds getting sent at one time. Is a new thread created for each async email request?

Basically I want to fire and forget emails from my site without waiting on the process to complete, this isn't for bulk email but for individual emails to users when required.

I'm expecting reasonable traffic and I'm just worried that thousands of threads are going to be created and my web server will crawl to a stop - or am I worrying about nothing?

Thanks!
0
badpilot
Asked:
badpilot
  • 3
  • 2
  • 2
1 Solution
 
Kyle AbrahamsSenior .Net DeveloperCommented:
A new thread would be created for each async call.

Instead, it's better to create one background thread and then send them synchronously from that thread.
0
 
badpilotAuthor Commented:
Thanks for your quick reply.

So at Application Start I should create a new background thread and send all mail through that?

Is it a good idea to leave a thread open like this all the time? Do I need to explicitly close this thread at application close? I take it I would need to deal with checking the thread is still live if for some reason it closed do to an exception etc.

Can you provide any code examples or links?

Many thanks!
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Create the thread when sending the email and then terminate it when the process is done. There is no need to have it open or live the whole time.

Here is an example:
http://www.pcreview.co.uk/forums/send-email-background-thread-t3829532.html
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
wdosanjosCommented:
Instead of spawning a thread for every email by directly instantiating System.Threading.Thread consider using System.Threading.ThreadPool instead, otherwise you run the risk of starving your system of resources.
Thread Pooling (C# and Visual Basic)
http://msdn.microsoft.com/en-us/library/h4732ks0.aspx
I hope this helps.
0
 
badpilotAuthor Commented:
Thread Pooling looks like what I'm looking for - two quick questions if you will:

The sample code on MSDN seems to suggest I don't need to instantiate a threadpool object - I can just add my task direct to the pool - I take it the framework handles everything else for me?

How do I handle errors with tasks added to the pool - do I just need to add a try catch to the sub that I pass to the pool?

Many thanks for you help.
0
 
wdosanjosCommented:
That's right, .NET handles everything for you so you don't need to instantiate ThreadPool (and since it's a static class it cannot be instantiated).  The callback method should handle errors with try/catch/finally  blocks.
0
 
badpilotAuthor Commented:
Perfect, exactly what I needed, thank you!
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now