/// <summary> /// Constructor, Intializing RelayCommands and Lists /// </summary> public StopwatchTimersVM() { // Relay commands to handle any task related to FirstStopwatch StartFirstStopwatchCommand = new RelayCommand(() => StartFirstStopwatch()); StopFirstStopwatchCommand = new RelayCommand(() => StopFirstStopwatch()); PauseFirstStopwatchCommand = new RelayCommand(() => PauseFirstStopwatch()); ResetFirstStopwatchCommand = new RelayCommand(() => ResetFirstStopwatch()); EmptyFirstStopWatchListCommand = new RelayCommand(() => EmptyFirstStopwatchList()); // Relay commands to handle any task related to SecondStopwatch StartSecondStopwatchCommand = new RelayCommand(() => StartSecondStopwatch()); StopSecondStopwatchCommand = new RelayCommand(() => StopSecondStopwatch()); PauseSecondStopwatchCommand = new RelayCommand(() => PauseSecondStopwatch()); ResetSecondStopwatchCommand = new RelayCommand(() => ResetSecondStopwatch()); EmptySecondStopWatchListCommand = new RelayCommand(() => EmptySecondStopwatchList()); // Relay commands to handle any task related to ThirdStopwatch StartThirdStopwatchCommand = new RelayCommand(() => StartThirdStopwatch()); StopThirdStopwatchCommand = new RelayCommand(() => StopThirdStopwatch()); PauseThirdStopwatchCommand = new RelayCommand(() => PauseThirdStopwatch()); ResetThirdStopwatchCommand = new RelayCommand(() => ResetThirdStopwatch()); EmptyThirdStopWatchListCommand = new RelayCommand(() => EmptyThirdStopwatchList()); // Filling the HoursList with 0-24 hours for (short i = 0; i <= 99; i++) { HoursList.Add(i); } // Filling the MinutesList and SecondsList with 0-60 minutes for (short i = 0; i <= 60; i++) { MinutesList.Add(i); SecondsList.Add(i); } StartFirstTimerCommand = new RelayCommand(() => StartFirstTimer()); PauseFirstTimerCommand = new RelayCommand(() => PauseFirstTimer()); StopFirstTimerCommand = new RelayCommand(() => StopFirstTimer()); StartSecondTimerCommand = new RelayCommand(() => StartSecondTimer()); PauseSecondTimerCommand = new RelayCommand(() => PauseSecondTimer()); StopSecondTimerCommand = new RelayCommand(() => StopSecondTimer()); StartThirdTimerCommand = new RelayCommand(() => StartThirdTimer()); PauseThirdTimerCommand = new RelayCommand(() => PauseThirdTimer()); StopThirdTimerCommand = new RelayCommand(() => StopThirdTimer()); }
private DateTime?ComputeNextTime() { // Test Cases: // @"* * * * * * *"; // @"23-37 * * * * * *"; // @"10-36/3 * * * * * *"; // @"10,23,27,33,55 * * * * * *"; // @"13 * * * * * *"; // @"* * * * * 1 *"; // @"1 2 3 5 12 1 *"; // @"1 2 3 * * 0 *"; // @"1 2 15 * * 0 *"; // @"* * * * * 0 *"; // @"20 * * * * 0 *"; // @"1 2 3 17 9 * 2011"; DateTime now = DateTime.Now; DateTime?nextTime = null; for (int y = YearsList.GetIndex(now.Year); y < YearsList.Count; y++) { for (int o = MonthsList.GetIndex(now.Month); o < MonthsList.Count; o++) { for (int d = 0; d < DaysList.Count; d++) { for (int w = WeekdaysList.GetIndex((int)now.DayOfWeek); w < WeekdaysList.Count; w++) { DateTime weekday = DateTime.MinValue; bool dayResult = DateTime.TryParse(string.Format(CultureInfo.InvariantCulture, @"{0}-{1}-{2} {3}:{4}:{5}", YearsList[y], MonthsList[o], DaysList[d], 0, 0, 0), out weekday); if (dayResult && (int)weekday.DayOfWeek == WeekdaysList[w]) { if (WeekdaysList[w] > (int)now.DayOfWeek) { DateTime target = DateTime.MinValue; bool parseResult = DateTime.TryParse(string.Format(CultureInfo.InvariantCulture, @"{0}-{1}-{2} {3}:{4}:{5}", YearsList[y], MonthsList[o], DaysList[d], HoursList[0], MinutesList[0], SecondsList[0]), out target); if (parseResult && target > now) { return(nextTime = target); } } else { for (int h = HoursList.GetIndex(now.Hour); h < HoursList.Count; h++) { for (int m = MinutesList.GetIndex(now.Minute); m < MinutesList.Count; m++) { for (int s = 0; s < SecondsList.Count; s++) { DateTime target = DateTime.MinValue; bool parseResult = DateTime.TryParse(string.Format(CultureInfo.InvariantCulture, @"{0}-{1}-{2} {3}:{4}:{5}", YearsList[y], MonthsList[o], DaysList[d], HoursList[h], MinutesList[m], SecondsList[s]), out target); if (parseResult && target > now) { return(nextTime = target); } } } } } } } } } } return(nextTime); }
/// <summary> /// 编译分析时程表达式 /// </summary> private void BuildExpression() { // Format : "* * * * * * *" // 第1列表示秒数0~59 每 秒用*或者*/1表示 // 第2列表示分钟0~59 每分钟用*或者*/1表示 // 第3列表示小时0~23 每小时用*或者*/1表示 // 第4列表示日期1~31 每 天用*或者*/1表示 // 第5列表示月份1~12 每 月用*或者*/1表示 // 第6列表示星期0~6 每 天用*表示*/1表示 0表示星期天 // 第7列表示月份2000~2099 每年用*或者*/1表示 // * 代表任意 * 每个 // / 代表每隔 /2 每隔2 // - 代表区间 1-7 从1至7 // , 表示单独 1,3 1和3 // 例子: 1-10/2 * * 17 9 * * 描述为 9月17日,在每分钟内,从1-10秒间每隔2秒触发一次 if (string.IsNullOrEmpty(CronExpressionString)) { throw new FormatException("CronExpressionString bad format."); } // 分割字符串 string[] columns = CronExpressionString.Trim().Split(new char[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); // 验证表达式字符串 CronExpressionHelper.ValidateCronExpression(columns); // 构造基础数据 for (int i = 0; i < columns.Length; i++) { string item = columns[i]; switch (i) { case 0: // 秒 SecondsList = CronExpressionHelper.ParseSecondExpression(item); break; case 1: // 分钟 MinutesList = CronExpressionHelper.ParseMinuteExpression(item); break; case 2: // 小时 HoursList = CronExpressionHelper.ParseHourExpression(item); break; case 3: // 日期 DaysList = CronExpressionHelper.ParseDayExpression(item); break; case 4: // 月份 MonthsList = CronExpressionHelper.ParseMonthExpression(item); break; case 5: // 星期 WeekdaysList = CronExpressionHelper.ParseWeekdayExpression(item); break; case 6: // 年份 YearsList = CronExpressionHelper.ParseYearExpression(item); break; default: CronExpressionValidationException.Throw("Maybe validation parsed error."); break; } } if (SecondsList == null || MinutesList == null || HoursList == null || DaysList == null || MonthsList == null || WeekdaysList == null || YearsList == null) { CronExpressionValidationException.Throw("CronExpression parsed collections null."); } // 列表内数据从小到大排序 SecondsList.Sort(); MinutesList.Sort(); HoursList.Sort(); DaysList.Sort(); MonthsList.Sort(); WeekdaysList.Sort(); YearsList.Sort(); }