工作中, 我們常常需要開發windows服務, 那麼, 一個可維護性好, 可擴充性高的的windows程式可以使我們省去不少的時間和精力.
同一種行為, 不同的實現邏輯, 典型的原廠模式.
public class TaskFactory
{
static Hashtable ht = Hashtable.Synchronized(new Hashtable());
public static IWindowTask GetTask(string classPath, IList parms) {
if (ht[classPath] != null)
return (IWindowTask)ht[classPath];
else {
//var taskInstance = Activator.CreateInstance(Type.GetType(classPath));
//var taskInstance = System.Reflection.Assembly.Load("MyWindowServiceFramwork").CreateInstance(classPath);
object taskInstance=null;
taskInstance = Type.GetType(classPath).GetConstructor(Type.EmptyTypes).Invoke(null);
ht[classPath] = taskInstance;
return (IWindowTask)taskInstance;
}
}
public static IWindowTask GetTask(string classPath) {
return GetTask(classPath, null);
}
}
枚舉
public enum InterValType {
second=1,
minute=2,
hour=3,
day=4,
week=5,
month=6
}
//給定時控制項添加一個任務類ID的屬性, 在下面要用到
class MyTime : System.Timers.Timer {
public int TaskID { get; set; }
}
private static void NewMethod1()
{
MyPractiseEntities enty = new MyPractiseEntities();
//獲得所有的有效任務.
var tasks = enty.MyTask.Where(x => x.enable == true);
foreach (var item in tasks)
{
MyTime mytime = new MyTime();
mytime.TaskID = item.ID;//將任務ID付給當前的定時類ID
mytime.Start(); //開始, 預設間隔是100毫秒
//達到時間間隔執行. 可以理解為每一個定時控制項都是一個新的線程.
mytime.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e)
{
//記錄邏輯操作耗時多少, 用以計算下一次運行此方法的時間間隔.
System.Diagnostics.Stopwatch stop = new System.Diagnostics.Stopwatch();
stop.Start();
MyPractiseEntities enty2 = new MyPractiseEntities();
var currentTimer = sender as MyTime;
//必須通過當前的時間控制項重新獲得資料庫物件. 如果直接寫item(見上一級的foreach), 那麼得到的item始終是tasks集合的最後一個.
var currentitem = enty2.MyTask.Where(x => x.ID == currentTimer.TaskID).FirstOrDefault();
//如果當前任務滿足執行條件
if (currentitem.LastRunTime <= DateTime.Now && (currentitem.NextRunTime == null || currentitem.NextRunTime <= DateTime.Now))
{
currentTimer.Stop();//暫停當前任務
//填充當前任務的所有參數
var itemParameter = enty2.MyTaskParameter.Where(x => x.TaskID == currentitem.ID).ToList();
Func<IList<MyTaskParameter>, object[]> toObjects=x=>{
object[] objs =new object[ x.Count];
for (int i = 0; i < x.Count; i++)
{
objs[i] = x[i].ParmValue;
}
return objs;
};
//獲得並執行
//使用工廠反射出需要執行的任務類,並轉化為介面對象/
IWindowTask itask = TaskFactory.GetTask(currentitem.ClassPath);
//執行
itask.Run(toObjects(itemParameter));
System.Threading.Thread.Sleep(2000);
//更新下一次執行的時間
Func<int, InterValType, double> plusSecond = (intervalue, intervalueType) =>
{
var temp = 0;
switch ((int)intervalueType)
{
case 1: temp = currentitem.Interval.Value; break;
case 2: temp = currentitem.Interval.Value * 60; break;
case 3: temp = currentitem.Interval.Value * 60 * 60; break;
case 4: temp = currentitem.Interval.Value * 60 * 60 * 24; break;
}
return temp;
};
//讀取設定檔來獲得時間間隔
var tempsecond = plusSecond(currentitem.Interval.Value, (InterValType)currentitem.IntervalType.Value);
DateTime d1 = currentitem.NextRunTime.Value;
currentitem.NextRunTime = d1.AddSeconds(tempsecond);
currentitem.LastRunTime = DateTime.Now;
enty2.SaveChanges();//更新任務的狀態資訊
stop.Stop();//監視停止
//時間控制項下一次執行的時間間隔為配置間隔減去這次運行時間長度.
currentTimer.Interval = tempsecond * 1000 - stop.ElapsedMilliseconds;
//開始時間控制項
currentTimer.Start();
Console.WriteLine(); Console.WriteLine();
}
};
}
}