views:

5618

answers:

9

Hi, Is it possible to use BackGroundWorker thread in asp.net 2.0 for the following scenario, so that the user at the browser's end does not have to wait for long time??

Scenario

  1. Browser requests a page, say SendEmails.aspx
  2. SendEmails.aspx page creates a BackgroundWorker thread, and supplies the thread with enough context to create and send emails.
  3. Browser receives the response from the ComposeAndSendEmails.aspx, saying that emails are being sent.
  4. Meanwhile, the background thread is engaged in a process of creating and sending emails which could take some considerable time to complete.

Thanks

Edit

My main concern is about keeping the BackgroundWorker thread running, trying to send, say 50 emails while the asp.net workerprocess threadpool thread is long gone.

A: 

Sounds to me like you are looking for something more like AJAX.

Geoffrey Chetwood
+1  A: 
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.

Darren Kopp
+1  A: 

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.

Gulzar
+5  A: 

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.

Dillie-O
Well you explained it in much more detail. Gave you an upvote. :)
Gulzar
+1 for the funny edit. I've felt that way a lot.
David Stratton
A: 

So is there something about asp.net that prevents BackgroundWorker threads from being used like in wpf or silverlight??. I get only few hits when i look for this topic on google.

kudlur
+2  A: 

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.

Eric Z Beard
This is often impossible on hosted environment. Then Cache removal trick do best job for me.
dario-g
+1  A: 

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

FlySwat
+1  A: 

If you want using multitheading in your asp page, you might using simple threading model like this:

{
System.Threading.Thread _thread = new Thread(new ThreadStart(Activity_DoWork));
_thred.Start();
}
Activity_DoWork()
{
/*Do some things...
}

this methods is correct working with asp pages. ASP Page with BackgroundWorker not start while BackgroundWorker will finished. Sorry for my language

A: 

Do we have any way to handle the multiple call for BackgroundWorker while it is still busy? Means can we make any queue of those operation and give it to background workere once its done with current work?