zoukankan      html  css  js  c++  java
  • C# ——计时器

    C# 的计时器是通过多线程来实现的。主要思路是,创建一个做死循环的线程,让该线程周期性的委托UI线程

    法一:利用system.Timers命名空间下的Timer类,使用Elapsed事件另开一个线程(Timer类中的Interval方法可以循环触发Elapsed事件)

     1  private void Form1_Load(object sender, EventArgs e)
     2         { 
     4             /////////*******system.Timers命名空间下的Timer类,使用Elapsed事件另开一个线程*******/////////
     5             System.Timers.Timer TimersTime = new System.Timers.Timer();
     6             TimersTime.Elapsed += new ElapsedEventHandler(TimedLabelOne);
     7             TimersTime.Interval = 1000;
     8             TimersTime.Start();//aTimer.Enabled = true;
    10         }

     Elapsed事件中运行的方法

      private void TimedLabelOne(object source, ElapsedEventArgs e)
            {
                time1.BeginInvoke(new TimeClock(TimeClocks1));
            }
            public delegate void TimeClock();//创建委托
      public void TimeClocks1()
            {
                time1.Text = DateTime.Now.ToString("HH:mm:ss");
            }

    方法二:利用System.Threading.Timer类

     
            private void Form1_Load(object sender, EventArgs e)
            {
                   // 参数:
            //   callback:
            //     一个 System.Threading.TimerCallback 委托,表示要执行的方法。
            //
            //   state:
            //     一个包含回调方法中,要使用信息的对象或 null。
            //
            //   dueTime:
            //     在之前的延迟时间量 callback 调用,以毫秒为单位。 指定 System.Threading.Timeout.Infinite 以防止启动计时器。
            //     指定零 (0) 可立即启动计时器。
            //
            //   period:
            //     调用之间的时间间隔 callback, ,以毫秒为单位。 指定 System.Threading.Timeout.Infinite 可以禁用定期终止。
      System.Threading.Timer ThreadingTime = new System.Threading.Timer(new TimerCallback(TimedLabelTwo),null,Timeout.Infinite,1000);
     //     更改计时器的启动时间和方法调用之间的间隔,用 32 位有符号整数度量时间间隔。
            //
            // 参数:
            //   dueTime:
            //     延迟之前调用的回调方法时指定的时间量 System.Threading.Timer 构造的以毫秒为单位。 指定 System.Threading.Timeout.Infinite
            //     以防止计时器重新启动。 指定零 (0) 可立即重新启动计时器。
            //
            //   period:
            //     回调方法的调用之间的时间间隔时指定 System.Threading.Timer 构造的以毫秒为单位。 指定 System.Threading.Timeout.Infinite
            //     可以禁用定期终止。
                ThreadingTime.Change(0, 1000);
    
            }
    //委托UI线程给控件赋值
     public delegate void TimeClock();  
    private void TimedLabelTwo(object state)
            {
                time1.BeginInvoke(new TimeClock(TimeClocks2));
            }
     public void TimeClocks2()
            {
                time2.Text = DateTime.Now.ToString("HH:mm:ss");
            }

    法三:通过自己创建的线程循环调用自制的事件

            public delegate void TimeHandle(object sender);
            public event TimeHandle TimeSelf;
            private void Form1_Load(object sender, EventArgs e)
            {
                TimeSelf += new TimeHandle(TimedLabelThree);
                ThreadStart threadState = TimeRefresh;
                Thread thread = new Thread(threadState);
                thread.Start();
    
            }
            public void TimeRefresh()
            {    
                while(true)
                {
                    TimeSelf(this);
                    Thread.Sleep(1000);
                }                       
            }
            private void TimedLabelThree(object state)
            {
                label1.BeginInvoke(new TimeClock(TimeClocks3));
            }
            public delegate void TimeClock();
    public void TimeClocks3() { label1.Text = DateTime.Now.ToString("HH:mm:ss"); }

    法四:创建一个死循环的线程,周期性的委托UI线程里的控件

            private void Form1_Load(object sender, EventArgs e)
            {
                ThreadStart threadStart = test;
                Thread thread1 = new Thread(threadStart);
                thread1.Start();
            }
            public void test()
            {
                while(true)
                {
                    label2.BeginInvoke(new TimeClock(TimeClocks4));
                    Thread.Sleep(1000);
                }           
            }
            public delegate void TimeClock();
            public void TimeClocks4()
            {
                label2.Text = DateTime.Now.ToString("HH:mm:ss");
            }
  • 相关阅读:
    psql: FATAL: index "pg_opclass_oid_index" contains unexpected zero page at block 0 以及错误 rm: cannot remove ‘base/90112/95992_fsm’: Structure needs cleaning
    rabbitmq centos 7.4 主备安装,加入主节点失败Error: unable to perform an operation on node 'rabbit@mq2'. Please see diagnostics information and suggestions below.
    Centos 7 查看显卡型号
    ORA-600 16703 SYS.TAB$表被删除
    linux orcal启动
    windows 下redis配置一主两从三哨兵模式以及springboot集成
    Spring定时任务service无法注入问题
    web缓存欺骗
    Automate Cache Poisoning Vulnerability
    bugbounty
  • 原文地址:https://www.cnblogs.com/f-t-q/p/10953598.html
Copyright © 2011-2022 走看看