Exemple #1
0
        /// <summary>
        /// 达到时间间隔时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            lock (removeList) {
                TimeModel temModel = null;
                foreach (var id in removeList)
                {
                    idModelDic.TryRemove(id, out temModel);
                }
                removeList.Clear();
            }
            //Console.WriteLine(DateTime.Now.Ticks);
            //Console.WriteLine("执行了一次");
            foreach (var model in idModelDic.Values)
            {
                //DateTime.Now.Ticks
                if (model.time <= DateTime.Now.Ticks)
                {
                    model.Run();
                    removeList.Add(model.id);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// 添加定时任务  指定延迟时间  比如延迟  30s  执行
        /// </summary>
        public void AddTimeEvent(long delayTime, TimeDelegate timDel)
        {
            TimeModel model = new TimeModel(id.Add_Get(), DateTime.Now.Ticks + delayTime, timDel);

            idModelDic.TryAdd(model.id, model);
        }