WindowForm timer control

Timer component Timer timer interval time-every this interval will trigger an event

いちSystem.Windows.Forms.Timer
Windows application occupies a thread to modify UI elements, based on the UI
attribute interval time interval ms 1000ms=1s
event tick
application: dynamic time display
If it is a Windows application, use the timer function System. Windows.Forms.Timer is the first choice.

//每隔指定的时间间隔就会触发
  private void timer1_Tick(object sender, EventArgs e)
   {
    
    
       lblDateTime.Text = DateTime.Now.ToString();
   }

   private void FrmTimer_Load(object sender, EventArgs e)
   {
    
    
       lblDateTime.Text = DateTime.Now.ToString();
       timer1.Enabled = true;	//启动计时器
   }

If the single execution time exceeds the interval time, it will affect the next trigger. The accuracy is poor.
· · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · ··········································
System.Timers.Timer
based on service The timer is lightweight and the
time interval triggers the Elapsed event—execute the operation—not executed by the UI thread

Property
Interval Time interval
AutoReset false will stop when triggered once true. By default , the UI element can
not be modified directly . It
can be done through the invoke() of the UI element, and the UI element is modified in the delegate ---- modify the UI element

private void FrmTimer_Load(object sender, EventArgs e)
  {
    
    
      System.Timers.Timer timer3 = new System.Timers.Timer();
      timer3.Interval = 1000;
      //timer3.AutoReset = false;		//只会引发一次,就停止了
      timer3.Elapsed += Timer3_Elapsed; //事件
      timer3.Start(); 					//开启计时器
  }
  int count = 0; //触发次数的终点
  private void Timer3_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  {
    
    
	  //不能直接修改UI元素的内容
      //lblDt2.Text= DateTime.Now.ToString();
      count += 1;
	  //用委托来进行修改元素内容
      Action act = UpdateTime;
      this.Invoke(act);
      if(count >10)
      {
    
    
          System.Timers.Timer timer = (System.Timers.Timer)sender;
          timer.Stop();			//停止计时器
      }
  }
//委托方法
  private void UpdateTime()
  {
    
    
      lblDatatime.Text = DateTime.Now.ToString();
  }

Time-consuming operation, will not make the UI unresponsive, will not affect the next trigger

さんSystem.Threading.Timer
Multi-threaded timer is lightweight and high precision

Provides a mechanism for executing methods on thread pool threads at specified time intervals.
A single run is too long and will not affect the next trigger . The callback method performs the operation

//System.Threading.Timer timer4;     
 private void FrmTimer_Load(object sender, EventArgs e)
  {
    
    
      //多线程
      //period  时间间隔  0或-1 只会执行一 次,然后就停止
      //Change 可以让计时器重新启动
      //停止 ①period  0 -1   ②timer4.Dispose() 
      int count2 = 0;
      timer4 = new System.Threading.Timer(new System.Threading.TimerCallback(o => {
    
    
          count2 += 2;
          Action<int> act = ShowCount;
          this.Invoke(act, count2);
      }), null, 1000,1000);
	//参数 回调函数,执行对象,延迟启动时间,触发时间间隔
   //timer4.Change(2000, 2000);	//改变延迟启动时间和时间间隔
  }
//委托函数
  private void ShowCount(int cout)
  {
    
    
      txtCount.Text = cout.ToString();
      if (cout > 50)
          timer4.Dispose();
  }

It is not possible to modify the UI elements directly, nor is it implemented by the invoke() of the UI element running on the UI thread

Guess you like

Origin blog.csdn.net/asdasd1fdsyrt/article/details/109477081