1

I have a set of projects that need to run only once at specific times every day. I was thinking to run each project as a webservice and use Timer or Task.Delay to schedule the work, but is timer/task good for scheduling work with long idle time?

Here is my scheduler configuration, and how timer is created. All configs are stored in DB.

public class ScheduledTaskStructure : IDisposable
{
    private Timer _timer;

    public void Start()
    {
        _timer = new Timer();
        _timer.Interval (new TimeSpan(24,0,0)).TotalMilliseconds;
        _timer.AutoReset = false;
        _timer.Elapsed += _timer_Elapsed;
    }

    private Assembly _Assembly;
    private object _instance;
    private Type _classType;
    MethodInfo _MethodInfo;
    void _timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        _timer.Stop();

        if (_Assembly == null)
        {
            _Assembly = Assembly.LoadFrom(ModuleName);
            _instance = _Assembly.CreateInstance(ClassName, true);
            _classType = _Assembly.GetType(ClassName, true, true);
            _MethodInfo = _classType.GetMethod(MethodName);
        }

        _MethodInfo.Invoke(_instance, null);

        _timer.Start();
    }

    public void Stop()
    {
        if (_timer != null)
        {
            _timer.Stop();
            _timer = null;
        }
    }

    public volatile bool IsRunning { get; set; }

    #region IDisposable Members

    public void Dispose()
    {
        _timer = null;
    }

    #endregion

    public string Task { get; set; }

    public DateTime NextRun { get; set; }
    public string TimeInterval { get; set; }
    public string ModuleName { get; set; }
    public string ClassName { get; set; }
    public string MethodName { get; set; }
    public string Status { get; set; }
    public bool IsUsed { get; set; }
}
Helic
  • 907
  • 1
  • 10
  • 25
  • 4
    Neither - IIS is liable to kill an Application Pool process for any reason without automatically starting it up again. You should use a Windows Service for anything that runs periodically, though you can hack it with a windows Scheduled Task which then invokes a program, for example. – Dai Apr 30 '15 at 08:14
  • Possible duplicate of: http://stackoverflow.com/questions/28364995/is-there-a-way-to-schedule-a-task-at-a-specific-time-or-with-an-interval?rq=1 ? – maam27 Apr 30 '15 at 08:15
  • possible duplicate of [Best way to run scheduled tasks](http://stackoverflow.com/questions/542804/best-way-to-run-scheduled-tasks) – Tomas Kubes Apr 30 '15 at 08:22

1 Answers1

4

Using Timer like this in general isn't great, and in IIS it is worse - IIS will specifically kill idle processes (unless you configure it not to or ping it to keep it alive).

In any-case you're really running and needlessly taking resource for the 23:59:00 that your program isn't actually doing anything, and exposing it to the chance it'll get killed or whatever in the meantime.

There are better constructs for running things at an appointed time. Simplest of which would be to just add your application into the Windows Task Scheduler.

For more complex requirements writing a windows service to interrogate your scheduling configuration and run processes at the desired time is probably the way to go.

Jon Egerton
  • 40,401
  • 11
  • 97
  • 129
  • 1
    The reason I am thinking to do the scheduling in code is that we have a big number of projects like this and I think doing it in code is good to centralize the control. – Helic Apr 30 '15 at 08:25
  • 1
    Using the task scheduler on a server, maybe even multiple server instances, may very well turn into a maintenance nightmare. Not if you have a single scheduled task, of course, but if the number and types of tasks and/or the configuration vary on a regular basis, a more sophisticated solution is in order. – bstenzel Apr 30 '15 at 08:29
  • @bstenzel I stored my scheduling configs in the database together with the assembly name, class name and method name to invoke for each project. Then I have a wrapper class that contains a timer/task, and in the timer elapse method, I use reflection to invoke the project. – Helic Apr 30 '15 at 08:36
  • @Helic: Well you didn't say any of that in the question! - Updated my answer. – Jon Egerton Apr 30 '15 at 09:01
  • @JonEgerton I was more concerned on running a timer with long idle time, such as a day. That is why I didn't put other details in the question. Let me modify the question – Helic Apr 30 '15 at 09:07