代码
using System; using System.Threading; using System.Collections.Generic;
namespace Rocky { #region Task public class Task : IDisposable { #region Fields private string taskName; private Timer timer; private TimerCallback execTask; private ISchedule schedule; private DateTime lastExecuteTime; private DateTime nextExecuteTime; #endregion
#region Properties /// <summary> /// 任务名称 /// </summary> public string Name { set { taskName = value; } get { return taskName; } } /// <summary> /// 执行任务的计划 /// </summary> public ISchedule Shedule { get { return schedule; } } /// <summary> /// 该任务最后一次执行的时间 /// </summary> public DateTime LastExecuteTime { get { return lastExecuteTime; } } /// <summary> /// 任务下执行时间 /// </summary> public DateTime NextExecuteTime { get { return nextExecuteTime; } } #endregion
#region Methods /// <summary> /// 构造函数 /// </summary> /// <param name="schedule">为每个任务制定一个执行计划</param> public Task(TimerCallback callback, ISchedule schedule) { if (callback == null || schedule == null) { throw new ArgumentNullException(); } this.execTask = callback; this.schedule = schedule; execTask += new TimerCallback(Execute); TaskScheduler.Register(this); }
/// <summary> /// 任务内容 /// </summary> /// <param name="state">任务函数参数</param> private void Execute(object state) { lastExecuteTime = DateTime.Now; if (schedule.Period == Timeout.Infinite) { nextExecuteTime = DateTime.MaxValue; //下次运行的时间不存在 } else { TimeSpan period = new TimeSpan(schedule.Period * 1000); nextExecuteTime = lastExecuteTime + period; } if (!(schedule is CycExecution)) { this.Close(); } }
public void Start() { Start(null); } public void Start(object execTaskState) { if (timer == null) { timer = new Timer(execTask, execTaskState, schedule.DueTime, schedule.Period); } <;span style="color: #0000ff;">else { timer.Change(schedule.DueTime, schedule.Period); } } public void RefreshSchedule() { if (timer != null) { timer.Change(schedule.DueTime, schedule.Period); } } public void Stop() { if (timer != null) { timer.Change(Timeout.Infinite, Timeout.Infinite); } }
public void Close() { ((IDisposable)this).Dispose(); }
void IDisposable.Dispose() { if (execTask != null) { taskName = null; if (timer != null) { timer.Dispose(); timer = null; } execTask = null; TaskScheduler.Deregister(this); } }
public override string ToString() { return taskName; } #endregion } #endregion
#region TaskScheduler /// <summary> /// 任务管理中心 /// 使用它可以管理一个或则多个同时运行的任务 /// </summary> public static class TaskScheduler { private static List<Task> taskScheduler;
public static int Count { get { return taskScheduler.Count; } }
static TaskScheduler() { taskScheduler = new List<Task>(); }
/// <summary> /// 查找任务 /// </summary> /// <param name="name"></param> /// <returns></returns> public static Task Find(string name) { return taskScheduler.Find(task => task.Name == name); }
public static IEnumerator<Task> GetEnumerator() { return taskScheduler.GetEnumerator(); }
/// <summary> /// 终止任务 /// </summary> public static void TerminateAllTask() { lock (taskScheduler) { taskScheduler.ForEach(task => task.Close()); taskScheduler.Clear(); taskScheduler.TrimExcess(); } }
internal static void Register(Task task) { lock (taskScheduler) { taskScheduler.Add(task); } } internal static void Deregister(Task task) { lock (taskScheduler) { taskScheduler.Remove(task); } } } #endregion
#region ISchedule /// <summary> /// 计划立即执行任务 /// </summary> public struct ImmediateExecution : ISchedule { public DateTime ExecutionTime { get { return DateTime.Now; } set { } } public long DueTime { get { return 0; } } public long Period { get { return Timeout.Infinite; } } }
/// <summary> /// 计划在某一未来的时间执行一个操作一次,如果这个时间比现在的时间小,就变成了立即执行的方式 /// </summary> public struct ScheduleExecutionOnce : ISchedule { private DateTime schedule;
public DateTime ExecutionTime { get { return schedule; } set { schedule = value; } } /// <summary> /// 得到该计划还有多久才能运行 /// </summary> public long DueTime { get { long ms = (schedule.Ticks - DateTime.Now.Ticks) / 10000; if (ms < 0) { ms = 0; } return ms; } } public long Period { get { return Timeout.Infinite; } }
/// <summary> /// 构造函数 /// </summary> /// <param name="schedule">计划开始执行的时间</param> public ScheduleExecutionOnce(DateTime time) { schedule = time; } }
/// <summary> /// 周期性的执行计划 /// </summary> public struct CycExecution : ISchedule { private DateTime schedule; private TimeSpan period;
public DateTime ExecutionTime { get { return schedule; } set { schedule = value; } } public long DueTime { get { long ms = (schedule.Ticks - DateTime.Now.Ticks) / 10000; if (ms < 0) { ms = 0; } return ms; } } public long Period { get { return period.Ticks / 10000; } }
/// <summary> /// 构造函数,马上开始运行 /// </summary> /// <param name="period">周期时间</param> public CycExecution(TimeSpan period) { this.schedule = DateTime.Now; this.period = period; } /// <summary> /// 构造函数,在一个将来时间开始运行 /// </summary> /// <param name="shedule">计划执行的时间</param> /// <param name="period">周期时间</param> public CycExecution(DateTime shedule, TimeSpan period) { this.schedule = shedule; this.period = period; } }
/// <summary> /// 计划的接口 /// </summary> public interface ISchedule { /// <summary> /// 返回最初计划执行时间 /// </summary> DateTime ExecutionTime { set; get; } /// <summary> /// 初始化执行时间于现在时间的时间刻度差 /// </summary> long DueTime { get; } /// <summary> /// 循环的周期 /// </summary> long Period { get; } } #endregion }
|