Stopping timer in its callback method

前端 未结 5 936
逝去的感伤
逝去的感伤 2020-12-05 01:23

I have a System.Threading.Timer that calls its appropriate event handler (callback) every 10 ms. The method itself is not reentrant and can

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 01:43

        //using Timer with callback on System.Threading namespace
        //  Timer(TimerCallback callback, object state, int dueTime, int period);
        //      TimerCallback: delegate to callback on timer lapse
        //      state: an object containig information for the callback
        //      dueTime: time delay before callback is invoked; in milliseconds; 0 immediate
        //      period: interval between invocation of callback; System.Threading.Timeout.Infinity to disable
        // EXCEPTIONS:
        //      ArgumentOutOfRangeException: negative duration or period
        //      ArgumentNullException: callback parameter is null 
    
        public class Program
        {
            public void Main()
            {
                var te = new TimerExample(1000, 2000, 2);
            }
        }
    
        public class TimerExample
        {
            public TimerExample(int delayTime, int intervalTime, int treshold)
            {
                this.DelayTime = delayTime;
                this.IntervalTime = intervalTime;
                this.Treshold = treshold;
                this.Timer = new Timer(this.TimerCallbackWorker, new StateInfo(), delayTime, intervalTime);
            }
    
            public int DelayTime
            {
                get;
                set;
            }
    
            public int IntervalTime
            {
                get;
                set;
            }
    
            public Timer Timer
            {
                get;
                set;
            }
    
            public StateInfo SI
            {
                get;
                set;
            }
    
            public int Treshold
            {
                get;
                private set;
            }
    
            public void TimerCallbackWorker(object state)
            {
                var si = state as StateInfo;
    
                if (si == null)
                {
                    throw new ArgumentNullException("state");
                }
    
                si.ExecutionCounter++;
    
                if (si.ExecutionCounter > this.Treshold)
                {
                    this.Timer.Change(Timeout.Infinite, Timeout.Infinite);
                    Console.WriteLine("-Timer stop, execution reached treshold {0}", this.Treshold);
                }
                else
                {
                    Console.WriteLine("{0} lapse, Time {1}", si.ExecutionCounter, si.ToString());
                }
            }
    
            public class StateInfo
            {
                public int ExecutionCounter
                {
                    get;
                    set;
                }
    
                public DateTime LastRun
                {
                    get
                    {
                        return DateTime.Now;
                    }
                }
    
                public override string ToString()
                {
                    return this.LastRun.ToString();
                }
            }
        }
    
        // Result:
        // 
        //  1 lapse, Time 2015-02-13 01:28:39 AM
        //  2 lapse, Time 2015-02-13 01:28:41 AM
        //  -Timer stop, execution reached treshold 2
        // 
    

提交回复
热议问题