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) { if (model.Time <= DateTime.Now.Ticks) { model.Run(); removeList.Add(model.Id); } } }
/// <summary> /// 添加定时任务 指定延迟的时间 30s /// </summary> /// <param name="delayTime">毫秒</param> /// <param name="timeDelegate"></param> public void AddTimeEvent(long delayTime, TimeDelegate timeDelegate) { TimerModel model = new TimerModel(id.Add_Get(), DateTime.Now.Ticks + delayTime, timeDelegate); idModelDict.TryAdd(model.Id, model); }