Reputation: 13302
Our site has an admin page that allows administrators to kick off a process that copies a given template CMS page out to a couple hundred directories. I guess this process takes a few minutes to run so the page times out. So what I think we need it to do is have the submit button start the task, then have the page poll for results every X seconds until it completes.
I've been trying to figure out how to do this and have seen Phil Haack's popular "Dangers of Implementing Recurring Background Tasks in ASP.NET" post. While a decent argument could be made that an external application should perform this work and not the web server, in reality, it won't be used very often, a web interface will be desired by admins, and we already have a working version of the code, the browser just doesn't wait around long enough to report completion.
I came across HostingEnvironment.QueueBackgroundWorkItem and thought that might work, except it looks like the current HttpContext isn't available to it. I think this might interfere with the CMS API, but I was also planning to use the Session State for my flag to indicate when the task had completed.
I've seen pages, like travel websites, or the SSRS web interface, show a polling page while it does some long-running processing. I don't need anything fancy or intricate for this purpose, I just need to avoid the timeout. Am I on the wrong track with QueueBackgroundWorkItem? If not, how can I tell when the task has finished?
Upvotes: 0
Views: 488
Reputation: 13302
I resolved the CMS API dependencies on the HttpRequest issues so I didn't have to worry about that anymore. I ended up using HostingEnvironment.QueueBackgroundWorkItem
and passed it the current HttpSessionState. When the background thread Action starts, it places an object in session to indicate whether the task is still running, has an error, or has completed successfully. Meanwhile, the page uses jQuery AJAX requests poll an ASP.NET Page Method that reads that status object from session. For the simple purposes of this admin page, it works well. Since we're using a session state server, this should work in a farm too.
Upvotes: 0
Reputation: 1235
You can use a tool like HangFire in order to start and execute a background task in your server and track it. It could be used in combination with SignalR (as @Tracker1 suggested) for notifications to the client side when the tasks are completed.
Upvotes: 1
Reputation: 19344
You'll probably want to use one of the following...
Other options include Message Queue systems that allow tracking if a ticket is complete, or doing it yourself with a database.
Upvotes: 1