Executing a long-running method right before a page redirect in ASP.NET

I have a method that is quite data intensive and could take minutes. Here is some code:

Data.Common.Documents.Regenerate();
Response.Redirect("~/Members/ManageDocs.aspx");

What is the simplest way to execute this method in a background thread?

Answers


Be aware, that if your App Domain gets recycled by the IIS, you'll lose the thread and everything that goes with it. But if you're OK with that, you can use TPL (.NET 4):

Task.Factory.StartNew(() => Data.Common.Documents.Regenerate());

That's assuming you don't have to wait for completion, 'cause that would defeat the point of background processing in this particular case.


If it will take very long, don't do it in asp.net. Move the processing to a windows service or a scheduled job. Asp.net is not designed for long running jobs.

If you must use asp.net though; you could refactor your code to Redirect without a ThreadAbortException, flush the output, and then continue with this

Response.Redirect("~/Members/ManageDocs.aspx", false);
Response.Flush();
Data.Common.Documents.Regenerate();

Need Your Help

Got a EXC_CORPSE_NOTIFY symbol crash, how to track it?

ios objective-c crash nsarray

Here's the whole crash log. What's the meaning of EXC_CORPSE_NOTIFY exception notice?

Export from hawt.io's dashboard to databese

database monitoring hawtio jolokia

How I can export information from hawt.io's dashboard to database in realtime? I want to save history of cpu load and ect, and read they from database later. May be this all wrong way and better wr...