/// <summary>实例化一个不可重入的定时器</summary> /// <param name="callback">委托</param> /// <param name="state">用户数据</param> /// <param name="dueTime">多久之后开始。毫秒</param> /// <param name="period">间隔周期。毫秒</param> /// <param name="scheduler">调度器</param> public TimerX(WaitCallback callback, Object state, Int32 dueTime, Int32 period, String scheduler = null) { if (dueTime < 0) { throw new ArgumentOutOfRangeException(nameof(dueTime)); } //if (period < 0) throw new ArgumentOutOfRangeException("period"); Callback = new WeakAction <Object>(callback) ?? throw new ArgumentNullException(nameof(callback)); State = state; Period = period; NextTime = DateTime.Now.AddMilliseconds(dueTime); Scheduler = scheduler.IsNullOrEmpty() ? TimerScheduler.Default : TimerScheduler.Create(scheduler); Scheduler.Add(this); }
/// <summary>实例化一个绝对定时器</summary> /// <param name="callback">委托</param> /// <param name="state">用户数据</param> /// <param name="startTime">绝对开始时间</param> /// <param name="period">间隔周期。毫秒</param> /// <param name="scheduler">调度器</param> public TimerX(WaitCallback callback, Object state, DateTime startTime, Int32 period, String scheduler = null) { if (startTime <= DateTime.MinValue) { throw new ArgumentOutOfRangeException(nameof(startTime)); } //if (period < 0) throw new ArgumentOutOfRangeException("period"); Callback = new WeakAction <Object>(callback) ?? throw new ArgumentNullException(nameof(callback)); State = state; Period = period; Absolutely = true; var now = DateTime.Now; var next = startTime; if (period % 1000 == 0) { var s = period / 1000; while (next < now) { next = next.AddSeconds(s); } } else { while (next < now) { next = next.AddMilliseconds(period); } } NextTime = next; Scheduler = scheduler.IsNullOrEmpty() ? TimerScheduler.Default : TimerScheduler.Create(scheduler); Scheduler.Add(this); }