/// <summary> /// 达到事件间隔触发 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { lock (removeID) { TimerModel timeModel = null; foreach (var id in removeID) { idModelDic.TryRemove(id, out timeModel); } removeID.Clear(); } foreach (var model in idModelDic.Values) { if (model.time <= DateTime.Now.Ticks) { model.Run(); } } }
/// <summary> /// 达到时间间隔时候触发 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { lock (removeList) { TimerModel tmpModel = null; foreach (var id in removeList) { idModelDict.TryRemove(id, out tmpModel); } removeList.Clear(); } foreach (var model in idModelDict.Values) { // t1: 10 + 2 = 12 // t2: 11 // t2: 13 if (model.Time <= DateTime.Now.Ticks) { model.Run(); } } }
/// <summary> /// 添加定时任务 指定延迟的时间 40s /// </summary> /// <param name="delayTime">毫秒!!</param> /// <param name="timeDelegate"></param> public void AddTimerEvent(long delayTime, TimerDelegate timeDelegate) { TimerModel model = new TimerModel(id.Add_Get(), DateTime.Now.Ticks + delayTime, timeDelegate); idModelDict.TryAdd(model.Id, model); }