I'd like to submit my form and then let it go to another page (telling the user that they'll receive an email when it's finished) while running the stored procedure (SQL) in the background.
I don't want the user to wait 30 mins for the search to complete and then go to the other page. How can I accomplish this?
// Clicking on the Search button
protected void btnSearch_Click(object sender, EventArgs e)
{
Guid SearchGUID;
SearchGUID = Guid.NewGuid();
Hashtable htSearch = new Hashtable();
htSearch["value"] = 2;
// Run the stored procedure (takes 30 mins)
objSmart.RunSearch(htSearch);
// Redirect to the other page
Response.Redirect("Search.aspx?search=" + SearchGUID.ToString());
}
If your background task were going to take only 30~90 seconds, it could be handled with Async, the thread pool/Tasks library, or any number of other solutions. But once it starts taking several minutes, or in your case, 30+ minutes, you need a more robust out-of-process solution. ASP.NET's architecture is just not designed for long-running background threads. IIS will recycle your application pool/domain at random intervals to optimize resource usage, and while it will try its best to let your threads finish, that's not guaranteed. See Scot Hanselman's blog post on this subject for some background and suggestions.
Here are a few solutions that you could consider, depending on your application constraints:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With