I’m considering using a fire-and-forget pattern for sending emails from an ASP/C# application, so the user isn’t waiting around while it does its thing. I’ve used the pattern before, pretty much like this article explains;
All very simple stuff, but I really need to know about the performance considerations. Am I supposed to just keep creating new threads every time I need to send an email, and just trust in the framework to manage them? Or is there a way i can manually put the thread back in the pool after it’s finished?
DO NOT starve the application’s ThreadPool. Instead, use SmartThreadPool if you have a lot of threads to create in a single process. There’s a Fire & Forget example in the article.
But… the easiest way to pop off an email is to use QueueUserWorkItem:
You won’t need to worry about putting the thread back in the threadpool. Threadpool management is automatically handled via the framework when using the ThreadPool class.