C# 实现注册定时器管理 备忘

 public class TimerTaskManger
    {
        private readonly Dictionary<string, TimerTask> AllTimerMangerDic = new Dictionary<string, TimerTask>();


        public bool RegistTimer(string name, Action<object> action, int interval)
        {
            return RegistTimer(name, action, interval, 0);

        }

        public bool RegistTimer(string name, Action<object> action, int interval, int delayMillisecond)
        {
            return RegistTimer(name, action, null, interval, delayMillisecond);
        }

        public bool RegistTimer(string name, Action<object> action, object obj, int interval, int delayMillisecond)
        {
            if (string.IsNullOrEmpty(name) || action == null || this.AllTimerMangerDic.ContainsKey(name))
            {
                return false;
            }
            lock (AllTimerMangerDic)
            {
                AllTimerMangerDic.Add(name, new TimerTask(name, action, obj, interval, delayMillisecond));
            }
            return true;
        }

        public bool UnRegistTimer(string name)
        {
            lock (AllTimerMangerDic)
            {
                if (AllTimerMangerDic.ContainsKey(name))
                {
                    var timerTask = AllTimerMangerDic[name];
                    if (timerTask.IsRun)
                    {
                        timerTask.Stop();
                    }
                    AllTimerMangerDic.Remove(name);
                    return true;
                }
            }
            return false;
        }

        public bool SetTaskTimerEnabled(string name, bool flag)
        {
            lock (AllTimerMangerDic)
            {
                if (!AllTimerMangerDic.ContainsKey(name))
                {
                    return false;
                }
                var timerTask = AllTimerMangerDic[name];
                if (flag)
                {
                    if (!timerTask.IsRun)
                    {
                        timerTask.ResetStart();
                    }
                }
                else
                {
                    timerTask.Stop();
                }
            }
            return true;
        }
    }

    public class TimerTask
    {
        public readonly string TimerName;
        private Action<object> InvokeMethod;
        private object stateObj;
        private int interval;
        public int Interval
        {
            get { return interval; }
            set
            {
                if (value > 0)
                {
                    this.interval = value;
                }
            }
        }

        private int delayMillisecond;
        public int DelayMillisecond
        {
            get { return delayMillisecond; }
            set
            {
                if (value > 0)
                {
                    this.delayMillisecond = value;
                }
            }
        }

        private System.Threading.Timer threadTimer = null;

        public bool IsRun { get; private set; }

        public TimerTask(string timerName, Action<object> entrust, object obj, int timeInterval, int delayTime)
        {
            if (entrust == null)
            {
                throw new ArgumentNullException("entrust");
            }
            this.stateObj = obj;
            this.TimerName = timerName;
            this.InvokeMethod = entrust;
            this.interval = timeInterval;
            this.delayMillisecond = delayTime;

            threadTimer = new System.Threading.Timer(new System.Threading.TimerCallback(this.InvokeMethod), this.stateObj, this.delayMillisecond, this.interval);
            this.IsRun = true;
        }

        public bool Stop()
        {
            if (threadTimer != null)
            {
                threadTimer.Dispose();
            }
            this.IsRun = false;
            return true;
        }

        public bool ResetStart()
        {
            if (threadTimer != null)
            {
                threadTimer.Dispose();
            }
            threadTimer = new System.Threading.Timer(new System.Threading.TimerCallback(this.InvokeMethod), this.stateObj, this.delayMillisecond, this.interval);
            this.IsRun = true;
            return true;
        }
    }

后期在做更改

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

望天hous

你的鼓励是我最大动力~谢谢啦!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值