views:

34

answers:

3

I tried this:

    BackgroundWorker bw = new BackgroundWorker();

    bw.DoWork += (o, e) =>
    {
        SendConfEmail();
    };

   bw.RunWorkerAsync();

but it didn't work. SendConfEmail takes a while to run. I guess it's because BackgroundWorker is designed for winforms not webforms.

Any ideas how I can solve the problem?

A: 

Check this article:

Processing Long Running Tasks With Asynchronous Handlers and XMLHTTP

Developer Art
That seems to be client-side javascript orientated.
SLC
+1  A: 

Waiting for a background thread to get queued up on the CPU from within a request is going to be near pointless. You should probably queue your emails from all threads and service them from a separate process, or from within a dedicated thread spawned, say, within global.asax.

Will
Why is it pointless? I want the confirmation page to appear for the user without the 5 second delay it takes for the e-mail to send.
SLC
@slc Its pointless because you're trying to spawn a background thread in a heavily threaded application. Emphasis on "background". On load you're going to find your background thread won't get scheduled much at all. Plus, add in the indeterminate amount of time it will take to actually send the email, and you have wait times of less than a second to possibly much more. Think about other design possibilities. You can queue it up, and return immediately to a wait page, which posts back every 10 seconds until it sees your email has been sent. Its a common pattern, and for good reason.
Will
@slc also, "BackgroundWorker is designed for winforms not webforms." Not quite. BW uses background threads, so they get much less CPU time than the threads in IIS. Re my statement above. BTW, if you want to block the response until the email is sent so you can show a confirmation page, why use a background thread at all?
Will
A: 

I solved it eventually by using an AsyncCallback object.

A centralised mail server solution would be ideal, but this project was a time critical proof of concept that had to be completed in just five days.

SLC