private void Timer_Elapsed(object sender, ElapsedEventArgs e) { lock (removeList) { TimeModel tempModel = null; foreach (var id in removeList) { idModelDict.TryRemove(id, out tempModel); } removeList.Clear(); } foreach (var model in idModelDict.Values) { if (model.Time <= DateTime.Now.Ticks) { model.Run(); } } }
//添加定时任务 XX时间后执行 public void AddTimeEvent(long delayTime, TimeDelegate timeDelegate) { TimeModel model = new TimeModel(id.Add_Get(), DateTime.Now.Ticks + delayTime, timeDelegate); idModelDict.TryAdd(model.Id, model); }