1 public delegate void timeDelegate();// Timer获取时间 2 3 private void Form1_Load(object sender, EventArgs e) 4 { 5 System.Timers.Timer t = new System.Timers.Timer(1000); //设置时间间隔为5秒 6 t.Elapsed += new System.Timers.ElapsedEventHandler(Timer_TimesUp); 7 t.Enabled = true; //是否触发Elapsed事件 8 t.AutoReset = true; //每到指定时间Elapsed事件是触发一次(false),还是一直触发(true) 9 } 10 private void Timer_TimesUp(object sender, System.Timers.ElapsedEventArgs e) 11 { 12 timeDelegate td = new timeDelegate(timeChange); 13 this.BeginInvoke(td); 14 } 15 public void timeChange() 16 { 17 int hour = System.DateTime.Now.Hour; 18 int minute = System.DateTime.Now.Minute; 19 int second = System.DateTime.Now.Second; 20 textBox1.Text = hour.ToString(); 21 textBox2.Text = minute.ToString(); 22 textBox3.Text = second.ToString(); 23 }
时间: 2024-12-29 23:53:25