Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to run scheduled tasks [closed]

People also ask

Does Task Scheduler work when closed?

No, it won't execute. The Task Scheduler in Vista and 7 can be configured to run missed instances, but XP's cannot. See the checkbox below called Run task as soon as possible after a scheduled start is missed. However, all three can be set to wake the computer if it's asleep or hibernating.

Will scheduled tasks run if computer is locked?

as a scheduled task does not appear to work when the PC is locked.

Does Task Scheduler work when computer is sleeping?

The short answer is yes, it will defragment while in Sleep Mode.


This technique by Jeff Atwood for Stackoverflow is the simplest method I've come across. It relies on the "cache item removed" callback mechanism build into ASP.NET's cache system

Update: Stackoverflow has outgrown this method. It only works while the website is running but it's a very simple technique that is useful for many people.

Also check out Quartz.NET


All of my tasks (which need to be scheduled) for a website are kept within the website and called from a special page. I then wrote a simple Windows service which calls this page every so often. Once the page runs it returns a value. If I know there is more work to be done, I run the page again, right away, otherwise I run it in a little while. This has worked really well for me and keeps all my task logic with the web code. Before writing the simple Windows service, I used Windows scheduler to call the page every x minutes.

Another convenient way to run this is to use a monitoring service like Pingdom. Point their http check to the page which runs your service code. Have the page return results which then can be used to trigger Pingdom to send alert messages when something isn't right.


Create a custom Windows Service.

I had some mission-critical tasks set up as scheduled console apps and found them difficult to maintain. I created a Windows Service with a 'heartbeat' that would check a schedule in my DB every couple of minutes. It's worked out really well.

Having said that, I still use scheduled console apps for most of my non-critical maintenance tasks. If it ain't broke, don't fix it.


I've found this to be easy for all involved:

  • Create a webservice method such as DoSuchAndSuchProcess
  • Create a console app that calls this webmethod.
  • Schedule the console app in the task scheduler.

Using this methodology all of the business logic is contained in your web app, but you have the reliability of the windows task manager, or any other commercial task manager to kick it off and record any return information such as an execution report. Using a web service instead of posting to a page has a bit of an advantage because it's easier to get return data from a webservice.


Why reinvent the wheel, use the Threading and the Timer class.

    protected void Application_Start()
    {
        Thread thread = new Thread(new ThreadStart(ThreadFunc));
        thread.IsBackground = true;
        thread.Name = "ThreadFunc";
        thread.Start();
    }

    protected void ThreadFunc()
    {
        System.Timers.Timer t = new System.Timers.Timer();
        t.Elapsed += new System.Timers.ElapsedEventHandler(TimerWorker);
        t.Interval = 10000;
        t.Enabled = true;
        t.AutoReset = true;
        t.Start();
    }

    protected void TimerWorker(object sender, System.Timers.ElapsedEventArgs e)
    {
        //work args
    }

Use Windows Scheduler to run a web page.

To prevent malicous user or search engine spiders to run it, when you setup the scheduled task, simply call the web page with a querystring, ie : mypage.aspx?from=scheduledtask

Then in the page load, simply use a condition : if (Request.Querystring["from"] == "scheduledtask") { //executetask }

This way no search engine spider or malicious user will be able to execute your scheduled task.


This library works like a charm http://www.codeproject.com/KB/cs/tsnewlib.aspx

It allows you to manage Windows scheduled tasks directly through your .NET code.