C# 计时器用法(DispatcherTimer、System.Timers.Timer、System.Threading.Timer)

2021-05-22 21:30

阅读:315

标签:sdn   vat   window   int   soft   win   mes   event   callback   

首先,我觉得三种计时器最大的区别是:DispatcherTimer触发的内容会直接转到主线程去执行(耗时操作会卡住主线程),另外两个则是在副线程执行,如果需要修改界面,则需要手动转到主线程。

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 TestTimersTimer()
        {
            _timer = new System.Timers.Timer();
            _timer.Interval = 1000; //单位毫秒
            _timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
            _timer.Start();
        }

        private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                Trace.WriteLine("Test System.Timers.Timer");
                #region 此处展示的是把副线程的内容转到主线程
                System.Windows.Application.Current.Dispatcher.Invoke(
                         new Action(() =>
                         {
                             Trace.WriteLine("同步切换");
                         }));
                System.Windows.Application.Current.Dispatcher.BeginInvoke(
                         new Action(() =>
                         {
                             Trace.WriteLine("异步切换");
                         }));
                #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();
        }

 此处个人无关记载:Environment.TickCount 

C# 计时器用法(DispatcherTimer、System.Timers.Timer、System.Threading.Timer)

标签:sdn   vat   window   int   soft   win   mes   event   callback   

原文地址:https://www.cnblogs.com/zebra-bin/p/11164703.html


评论


亲,登录后才可以留言!