BackgroundWorker thread in ASP.NET

Solution 1:

If you don't want to use the AJAX libraries, or the e-mail processing is REALLY long and would timeout a standard AJAX request, you can use an AsynchronousPostBack method that was the "old hack" in the .net 1.1 days.

Essentially what you do is have your submit button begin the e-mail processing in an asynchronous state, while the user is taken to an intermediate page. The benefit to this is that you can have your intermediate page refresh as much as needed, without worrying about hitting the standard timeouts.

When your background process is complete, it will put a little "done" flag in the database/application variable/whatever. When your intermediate page does a refresh of itself, it detects this flag and automatically redirects the user to the "done" page.

Again, AJAX makes all of this moot, but if for some reason you have a very intensive or timely process that has to be done over the web, this solution will work for you. I found a nice tutorial on it here and there are plenty more out there.

I had to use a process like this when we were working on a "web check-in" type application that was interfacing with a third party application and their import API was hideously slow.

EDIT: GAH! Curse you Guzlar and your god-like typing abilities 8^D.

Solution 2:

You shouldn't do any threading from ASP.NET pages. Any thread that is long running is in danger of being killed when the worker process recycles. You can't predict when this will happen. Any long-running processes need to be handled by a windows service. You can kick off these processes by dropping a message in MSMQ, for example.

Solution 3:

ThreadPool.QueueUserWorkItem(delegateThatSendsEmails)

or on System.Net.Mail.SmtpServer use the SendAsync method.

You want to put the email sending code on another thread, because then it will return the the user immediately, and will just process, no matter how long it takes.

Solution 4:

It is possible. Once you start a new thread asynchronously from page, page request will proceed and send the page back to the user. The async thread will continue to run on the server but will no longer have access to the session.

If you have to show task progress, consider some Ajax techniques.

Solution 5:

What you need to use for this scenario is Asynchronous Pages, a feature that was added in ASP.NET 2.0

Asynchronous pages offer a neat solution to the problems caused by I/O-bound requests. Page processing begins on a thread-pool thread, but that thread is returned to the thread pool once an asynchronous I/O operation begins in response to a signal from ASP.NET. When the operation completes, ASP.NET grabs another thread from the thread pool and finishes processing the request. Scalability increases because thread-pool threads are used more efficiently. Threads that would otherwise be stuck waiting for I/O to complete can now be used to service other requests. The direct beneficiaries are requests that don't perform lengthy I/O operations and can therefore get in and out of the pipeline quickly. Long waits to get into the pipeline have a disproportionately negative impact on the performance of such requests.

http://msdn.microsoft.com/en-us/magazine/cc163725.aspx