zoukankan      html  css  js  c++  java
  • C# 计时器用法(DispatcherTimer、System.Timers.Timer、System.Threading.Timer)

    首先,我觉得三种计时器最大的区别是: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语言中内存堆和栈的区别
    (二)再议MII、RMII、GMII接口
    (一)MII/MDIO接口详解
    (四)单片机系统动态内存分配,任务调度思想
    (十)Linux内核中的常用宏container_of
    (八)C语言结构体和指针
    (七)C语言中的void 和void 指针类型
    (六)C语言之typedef详解
  • 原文地址:https://www.cnblogs.com/zebra-bin/p/11164703.html
Copyright © 2011-2022 走看看