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; }
}