C # timer usage (DispatcherTimer, System.Timers.Timer, System.Threading.Timer)

First of all, I think the biggest distinguish three timer is: Content DispatcherTimer trigger will go directly to the main thread to execute (time-consuming operation will be stuck main thread), the other two are in the sub-thread execution, if you need to modify the interface , you need to manually go to the main thread.

DispatcherTimer:

        DispatcherTimer _timer;
        public void TestDispatcherTimer()
        {
            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(1);
            _timer.Tick += _timer_Tick;
            _timer.Start();
        }
        private void _timer_Tick(object sender, EventArgs e)
        {
            try 
            {
                Trace.WriteLine("Test DispatcherTimer");
                _timer.Stop();
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Error");
                _timer.Stop();
                _timer.Start();
            }
        }        

System.Timers.Timer:

        System.Timers.Timer _timer;
        public  void Test Hours Hours () 
        { 
            _timer = New System.Timers.Timer (); 
            _timer.Interval = 1000 ; // 单位毫秒 
            _timer.Elapsed + = new System.Timers.ElapsedEventHandler (_timer_Elapsed); 
            _timer.Start (); 
        } 

        Private  void _timer_Elapsed ( Object transmitter System.Timers.ElapsedEventArgs e) 
        { 
            try 
            { 
                Trace.WriteLine ( " Test System.Timers.Timer" );
                 #Region shown here is the contents of the sub-thread to the main thread 
                System.Windows.Application.Current.Dispatcher.Invoke ( 
                         new new the Action (() => 
                         { 
                             Trace.WriteLine ( " synchronous handover " ); 
                         } )); 
                System.Windows.Application.Current.Dispatcher.BeginInvoke ( 
                         new new the Action (() => 
                         { 
                             Trace.WriteLine ( " asynchronous switching " ); 
                         })); 
                #endregion 
                _timer.Stop (); 
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Error");
                _timer.Stop();
                _timer.Start();
            }
        }

System.Threading.Timer:

        System.Threading.Timer _timer;
        private void TeseThreadingTimer()
        {
            _timer = new System.Threading.Timer(new System.Threading.TimerCallback(Out), null, 0, 1000); //各参数意思详见:https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.timer?redirectedfrom=MSDN&view=netframework-4.8
        }
        private void Out(object sender)
        {
            try
            {
                Trace.WriteLine("Test System.Threading.Timer");
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Error");
            }
        }
        private void Stop()
        {
            _timer.Dispose();
        }

 Here impersonal records: Environment.TickCount 

Guess you like

Origin www.cnblogs.com/zebra-bin/p/11164703.html