<p>Is it possible to use BackGroundWorker thread in <a href="http://en.wikipedia.org/wiki/ASP.NET" rel="nofollow noreferrer">ASP.NET</a> 2.0 for the following scenario, so that the user at the browser's end does not have to wait for long time?</p>

<h2>Scenario</h2>

<ol>
<li>The browser requests a page, say SendEmails.aspx</li>
<li>SendEmails.aspx page creates a BackgroundWorker thread, and supplies the thread with enough context to create and send emails.</li>
<li>The browser receives the response from the ComposeAndSendEmails.aspx, saying that emails are being sent.</li>
<li>Meanwhile, the background thread is engaged in a process of creating and sending emails which could take some considerable time to complete.</li>
</ol>

<p>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.</p>

[{'answer_id': 57847, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 3, 'selected': False, 'text': '<pre><code>ThreadPool.QueueUserWorkItem(delegateThatSendsEmails)\n</code></pre>\n\n<p>or on System.Net.Mail.SmtpServer use the SendAsync method.</p>\n\n<p>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.</p>\n'}, {'answer_id': 57850, 'author': 'Gulzar Nazim', 'author_id': 4337, 'author_profile': 'https://Stackoverflow.com/users/4337', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n\n<p>If you have to show task progress, consider some Ajax techniques.</p>\n'}, {'answer_id': 57858, 'author': 'Dillie-O', 'author_id': 71, 'author_profile': 'https://Stackoverflow.com/users/71', 'pm_score': 5, 'selected': True, 'text': '<p>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. </p>\n\n<p>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. </p>\n\n<p>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.</p>\n\n<p>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 <a href="http://www.simple-talk.com/dotnet/asp.net/asynchronous-client-script-callbacks/" rel="noreferrer">here</a> and there are plenty more out there. </p>\n\n<p>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.</p>\n\n<p>EDIT: GAH! Curse you Guzlar and your god-like typing abilities 8^D.</p>\n'}, {'answer_id': 57891, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 3, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 57901, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 2, 'selected': False, 'text': '<p>What you need to use for this scenario is Asynchronous Pages, a feature that was added in ASP.NET 2.0</p>\n\n<blockquote>\n  <p>Asynchronous pages offer a neat\n  solution to the problems caused by\n  I/O-bound requests. Page processing\n  begins on a thread-pool thread, but\n  that thread is returned to the thread\n  pool once an asynchronous I/O\n  operation begins in response to a\n  signal from ASP.NET. When the\n  operation completes, ASP.NET grabs\n  another thread from the thread pool\n  and finishes processing the request.\n  Scalability increases because\n  thread-pool threads are used more\n  efficiently. Threads that would\n  otherwise be stuck waiting for I/O to\n  complete can now be used to service\n  other requests. The direct\n  beneficiaries are requests that don\'t\n  perform lengthy I/O operations and can\n  therefore get in and out of the\n  pipeline quickly. Long waits to get\n  into the pipeline have a\n  disproportionately negative impact on\n  the performance of such requests.</p>\n</blockquote>\n\n<p><a href="http://msdn.microsoft.com/en-us/magazine/cc163725.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/magazine/cc163725.aspx</a></p>\n'}, {'answer_id': 431015, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>If you want using multitheading in your ASP page, you might using simple threading model like this:</p>\n\n<pre><code>{\n    System.Threading.Thread _thread = new Thread(new ThreadStart(Activity_DoWork));\n    _thred.Start();\n}\nActivity_DoWork()\n{\n    /*Do some things...\n}\n</code></pre>\n\n<p>This method is correct working with ASP pages. The ASP page with BackgroundWorker will not start while BackgroundWorker will finish.</p>\n'}, {'answer_id': 24147428, 'author': 'odinserj', 'author_id': 1317575, 'author_profile': 'https://Stackoverflow.com/users/1317575', 'pm_score': 2, 'selected': False, 'text': '<p>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 <a href="http://hangfire.io" rel="nofollow">http://hangfire.io</a>.</p>\n\n<ul>\n<li>It does not loose your jobs on recycling process, because it uses persistent storage to keep information about background jobs.</li>\n<li>It automatically retries your background jobs that were aborted or failed due to transient exception (SMTP Server connectivity errors).</li>\n<li>It allows you to easily debug background jobs through the integrated web interface.</li>\n<li>It is very easy to install/configure/use HangFire.</li>\n</ul>\n\n<p>There is also tutorial <a href="http://docs.hangfire.io/en/latest/tutorials/send-email.html" rel="nofollow">Sending Mail in Background with ASP.NET MVC</a> for using HangFire with <a href="http://aboutcode.net/postal/" rel="nofollow">Postal</a>.</p>\n'}]
['https://Stackoverflow.com/questions/57845', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1647/']