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:
What is the simplest way to execute this method in a background thread?
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();