主要代碼:
1. ITask.cs
2. TaskSchedule.cs
3. TimeCalculations.cs
4. TimerStrategyConfig.cs
5. TimerStrategyManager.cs
1. 所有用戶代碼都繼承該類ITask .
using BlueVision.ShianYuan.Framework; using System; namespace BlueVision.ShianYuan.TestProject { /// <summary> /// 測試用戶 /// </summary> public class TestService : ITask { /// <summary> /// 開始服務 /// </summary> protected override void OnStart(params object[] Params) { System.Windows.Forms.MessageBox.Show("處理一個任務完成!"); } protected override void OnStop() { System.Windows.Forms.MessageBox.Show("停止服務!"); } protected override void OnThrowException(Exception ex) { System.Windows.Forms.MessageBox.Show("出現異常!"); } } } using BlueVision.ShianYuan.Framework; using System; namespace BlueVision.ShianYuan.TestProject { /// <summary> /// 測試用戶 /// </summary> public class TestService : ITask { /// <summary> /// 開始服務 /// </summary> protected override void OnStart(params object[] Params) { System.Windows.Forms.MessageBox.Show("處理一個任務完成!"); } protected override void OnStop() { System.Windows.Forms.MessageBox.Show("停止服務!"); } protected override void OnThrowException(Exception ex) { System.Windows.Forms.MessageBox.Show("出現異常!"); } } }
之後,可以選擇執行該子類.
var service = new TestService(); //開始執行 service.Start(new TimerStrategy { TimerMode = TimerStrategyMode.EveryDay, Reentrant = true, StrategyByDay = new TimerStrategyByDay { TimePoints = new TimeSpan[] { new TimeSpan( 17, 20, 2 ), new TimeSpan( 17, 30, 4 ), new TimeSpan( 17, 40, 6 ), new TimeSpan( 17, 50, 8 ) } } }); //停止可以執行: service.Stop(); var service = new TestService(); //開始執行 service.Start(new TimerStrategy { TimerMode = TimerStrategyMode.EveryDay, Reentrant = true, StrategyByDay = new TimerStrategyByDay { TimePoints = new TimeSpan[] { new TimeSpan( 17, 20, 2 ), new TimeSpan( 17, 30, 4 ), new TimeSpan( 17, 40, 6 ), new TimeSpan( 17, 50, 8 ) } } });
//停止可以執行:
service.Stop();
或是
/// 定時說明 /// 只要調用StartSchedule,任務就自動按照定時規則開始定時 /// 定時策略為: 每天,在設定的TimeSpan時間點上都執行一次,而且是可重入的執行,Reetrant為可重入開關 TaskSchedule.StartSchedule("MyTaskFunction", new TestService(), new TimerStrategy { TimerMode = TimerStrategyMode.EveryDay, Reentrant = false, // 不可重入, 即在上一個未執行完成之前,不會執行其它到期的下次任務. StrategyByDay = new TimerStrategyByDay { TimePoints = new TimeSpan[] { new TimeSpan( 17, 20, 2 ), new TimeSpan( 17, 30, 4 ), new TimeSpan( 17, 40, 6 ), new TimeSpan( 17, 50, 8 ) } } }); // 停止所有的計劃任務. TaskSchedule.StopSchedule(); // 或停止指定的計劃任務 TaskSchedule.StopSchedule("MyTaskFunction"); 以下是時間策略的說明 // 以下表示, 每年的,2-29,5-31,7-30,4-29,6-29的10點,12,16,19點各執行一次. var strategy_A = new TimerStrategy { TimerMode = TimerStrategyMode.Date, Reentrant = false, // 不可重入, 即在上一個未執行完成之前,不會執行其它到期的下次任務. Dates = new List<DATETIME>(new DateTime[] // 年份無關緊要. { new DateTime(4,2,29), new DateTime(4,5,31), new DateTime(4,7,30), new DateTime(4,4,29), new DateTime(4,6,29) }), StrategyByDay = new TimerStrategyByDay { TimePoints = new TimeSpan[] { new TimeSpan(10,0,0), new TimeSpan(12,0,0), new TimeSpan(16,0,0), new TimeSpan(19,0,0), } } }; // 以下表示, 每個月的29,31號的10點,12,16,19點各執行一次.(沒有29,31號的月份就不執行.) var strategy_B = new TimerStrategy { TimerMode = TimerStrategyMode.EveryMonth, Days = new List<INT>(new int[] { 29, 31 }), StrategyByDay = new TimerStrategyByDay { TimePoints = new TimeSpan[] { new TimeSpan(10,0,0), new TimeSpan(12,0,0), new TimeSpan(16,0,0), new TimeSpan(19,0,0), } } }; // 以下表示, 每周的星期天,星期三的10,19點各執行一次. var strategy_C = new TimerStrategy { TimerMode = TimerStrategyMode.EveryWeek, Days = new List<INT>(new int[] { 0, 3 }), StrategyByDay = new TimerStrategyByDay { TimePoints = new TimeSpan[] { new TimeSpan(10,0,0), new TimeSpan(19,0,0), } } }; // 以下表示, 每年的 // 2月份的 2-10 // 3月份的 3-10, 3-30, 3-31, // 6月份的 6-10, 6-30 // 9月份的 9-10, 9-30 // 10月份的 10-10, 10-30, 10-31, // 以下各天的10,19點各執行一次. var strategy_D = new TimerStrategy { TimerMode = TimerStrategyMode.Month, Months = new List<INT>(new int[] { 2, 3, 6, 9, 10 }), Days = new List<INT>(new int[] { 10, 30, 31 }), StrategyByDay = new TimerStrategyByDay { TimePoints = new TimeSpan[] { new TimeSpan(10,0,0), new TimeSpan(19,0,0), } } }; // 以下表示, 每個月的倒數,第28天,第1天,第2天 的10,19點各執行一次. (Days最大只允許28,) // 如: 非閏年的2月,就是 2-1,2-27,2-28三天, // 如: 大月份的3月,就是 3-4,3-30,3-31三天 // 如: 小月份的4月,就是 4-3,4-29,4-30三天 var strategy_E = new TimerStrategy { TimerMode = TimerStrategyMode.LastDayOfMonth, Days = new List<INT>(new int[] { 28, 1, 2 }), StrategyByDay = new TimerStrategyByDay { TimePoints = new TimeSpan[] { new TimeSpan(10,0,0), new TimeSpan(19,0,0), } } }; // 以下表示, 每年的第365天,和366天的10,19點各執行一次. (Days最大只允許366,) // 例: 2012年就是 12-30,和12-31兩天, 閏年 // 2013年就是 12-31 非閏年,只執行一天. var strategy_F = new TimerStrategy { TimerMode = TimerStrategyMode.EveryYear, Days = new List<INT>(new int[] { 365, 366 }), StrategyByDay = new TimerStrategyByDay { TimePoints = new TimeSpan[] { new TimeSpan(10,0,0), new TimeSpan(19,0,0), } } }; // 以下表示, 每天的10,19點各執行一次. var strategy_G = new TimerStrategy { TimerMode = TimerStrategyMode.EveryDay, StrategyByDay = new TimerStrategyByDay { TimePoints = new TimeSpan[] { new TimeSpan(10,0,0), new TimeSpan(19,0,0), } } }; // 以下表示每隔1小時執行一次. var strategy_H = new TimerStrategy { TimerMode = TimerStrategyMode.EveryDay, Interval = new TimeSpan(1, 0, 0) }; /INT></INT></INT></INT></INT></INT></DATETIME>