static TaskMatch MatchTasks(CronTask task) { //enum TaskMatch { Unset=0x00,Minute = 0x01, Hour = 0x02, DayOfMonth = 0x04, Month = 0x08, DayOfWeek = 0x16 ,Complete = 0x31}; TaskMatch ret = TaskMatch.Unset; DateTime dt = DateTime.Now; // new DateTime(2011, 4, 26, 10, 17, 0);// DateTime.Today; //Console.WriteLine("Diff: " + ((TimeSpan)(dt - task.LastRun)).TotalSeconds.ToString()); if ((dt - task.LastRun).TotalSeconds < 60) { return(ret); } ParseMatch(task.Minute, dt.Minute, ref ret, TaskMatch.Minute); ParseMatch(task.Hour, dt.Hour, ref ret, TaskMatch.Hour); ParseMatch(task.DayOfMonth, dt.Day, ref ret, TaskMatch.DayOfMonth); ParseMatch(task.Month, dt.Month, ref ret, TaskMatch.Month); ParseMatch(task.DayOfWeek, (int)dt.DayOfWeek, ref ret, TaskMatch.DayOfWeek); return(ret); }
static void ParseMatch(String value, int expected, ref TaskMatch ret, TaskMatch match) { if (value[0] == '*') { ret |= match; return; } String [] values = value.Split(','); foreach (String val in values) { int iVal = Convert.ToInt32(val); if (iVal == expected) { ret |= match; return; } } return; }
void OnTimerEvent(object source, ElapsedEventArgs args) { foreach (CronTask task in CronSettings.Tasks) { TaskMatch completeMask = TaskMatch.Complete; /// TaskMatch.Minute | TaskMatch.Hour | TaskMatch.Month | TaskMatch.DayOfMonth | TaskMatch.DayOfWeek; TaskMatch final = MatchTasks(task); if ((final & completeMask) == completeMask) { Console.WriteLine("Running Cmd[" + task.UniqueID.ToString() + "]: " + task.Command); Process proc = new Process(); proc.StartInfo.UseShellExecute = false; proc.StartInfo.CreateNoWindow = false; proc.StartInfo.FileName = "C:\\Windows\\System32\\cmd.exe"; proc.StartInfo.Arguments = " /C \"" + task.Command + "\""; proc.Start(); task.LastRun = DateTime.Now; //Floor to last valid minute(max resolution of timer) TimeSpan tmp = new TimeSpan(0, 0, task.LastRun.Second); task.LastRun -= tmp; } } }
static void ParseMatch(String value, int expected, ref TaskMatch ret,TaskMatch match) { if (value[0] == '*') { ret |= match; return; } String []values = value.Split(','); foreach (String val in values) { int iVal = Convert.ToInt32(val); if (iVal == expected) { ret |= match; return; } } return; }