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 

  • 相关阅读:
    VR虚拟现实眼镜那些事
    C#使用MysqlBackup.Net 备份MySQL数据库
    AgileConfig服务端搭建
    IE浏览器,各版本的请求头信息
    搜索算法
    一个完整的信号采集系统项目开发流程
    Linux内核模块简介
    搭建wordpress开发环境
    AbstractRoutingDataSource+AOP+JNDI实现spring动态数据源
    Linux 挂载磁盘记录
  • 原文地址:https://www.cnblogs.com/zebra-bin/p/11164703.html
Copyright © 2011-2022 走看看