BackgroundWorker thread in ASP.NET

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. The 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. The 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.

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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.

Method 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.

Method 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.

Method 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.

Method 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

Method 6

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 method is correct working with ASP pages. The ASP page with BackgroundWorker will not start while BackgroundWorker will finish.

Method 7

5 years later, but problems the same… If you want to perform fire-and-forget operations from your application and forget about all difficulties related to background job processing in ASP.NET applications, you can use http://hangfire.io.

  • It does not loose your jobs on recycling process, because it uses persistent storage to keep information about background jobs.
  • It automatically retries your background jobs that were aborted or failed due to transient exception (SMTP Server connectivity errors).
  • It allows you to easily debug background jobs through the integrated web interface.
  • It is very easy to install/configure/use HangFire.

There is also tutorial Sending Mail in Background with ASP.NET MVC for using HangFire with Postal.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x