Launching a long-running background task - from ASP.NET or a scheduled job

marc_s picture marc_s · Jan 23, 2015 · Viewed 7.3k times · Source

I have a fairly long-running process (several minutes) that I need to run roughly once a month - but not on a fixed schedule, but after a user clicks Go in a ASP.NET Webforms GUI page.

Since ASP.NET really isn't designed to handle long-running background tasks, my idea was to put this into a separate console app. But how to launch that as needed?

What happens if I use Process.Start(....) from my ASP.NET page's code-behind? I would like to avoid blocking the whole Web UI for 20 minutes or so... and also: even if it doesn't block my UI, what happens to my long-running task if the ASP.NET app pool recycles while it's still running?

Another idea was to have a frequently running job (runs every 2 minutes) check for some kind of a flag (e.g. existence of some database entries), and if needed, that job would then launch the long-running task.

But the same question: if I launch my 20-minute task from a job using Process.Start() - does that block the caller?

It seems like a bit of overkill to schedule that long running tasks five times a day since it typically is run only once a month - but at the same time, the user expects to have his results within a reasonable amount of time (less than 1 hour, if ever possible) after scheduling the process - so I cannot really just schedule it to run once at night either ...

Answer

ravish.hacker picture ravish.hacker · Mar 29, 2016

Hangfire is what you are looking for. Best part is it comes with a built in dashboard.

You might have to write some logic on the top of it.

You can find it here. http://hangfire.io/

enter image description here

enter image description here