zoukankan      html  css  js  c++  java
  • DispatcherTimer和Timer的区别

    两者区别是 Timer在非UI线程跑的,DispatcherTimer是在UI线程跑的,

    DispatcherTimer 可以直接更新UI

    Timer必须使用this.Dispatcher.BeginInvoke去更新UI        

    private void DisPatcherTimerMethod()

            {
                DispatcherTimer timer = new DispatcherTimer();
                timer.Interval = TimeSpan.FromMilliseconds(1000);
                timer.Tick += Timer_Tick;
                timer.Start();
            }
            private void Timer_Tick(object sender, EventArgs e)
            {
                this.label1.Text = DateTime.Now.ToString();
            }
            private void TimerMethod()
            {
                System.Timers.Timer tmr = new System.Timers.Timer(1000); //1秒一个循环
                tmr.Elapsed += tmr_Elapsed;
                tmr.Start();
            }
            private void tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                this.Dispatcher.BeginInvoke(new Action(() =>
                {
                    this.label2.Text = DateTime.Now.ToString();
                }), null);
            }
  • 相关阅读:
    接口和抽象类的异同点
    实体对象间传值克隆
    什么是反射&反射的8个类的用法
    ERP中反射的两个实例
    基础02 Java 跨平台原理
    基础01 dos命令
    lists删除
    多字段 java对象排序
    Java对象排序
    MySQL表中数据的迁移
  • 原文地址:https://www.cnblogs.com/LCLBook/p/11504500.html
Copyright © 2011-2022 走看看