多线程TimerCallBack

来源:互联网 发布:7支持4g十网络吗 编辑:程序博客网 时间:2024/06/06 20:22

在窗体的Lable控件中显示时间。


Form中内容:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Timers;using timeLib;namespace TEST{       public partial class Form1 : Form        {            public delegate void UpdateTextHandler(DateTime now);    //UpdateTextHandler委托声明               timerLib libTime;               public Form1()              {                     InitializeComponent();             libTime = new timerLib();             libTime.OnTimeChange += new OnTimerChangeHandler(Ontimechange);         //OnTimeChange注册                 libTime.start();              }             private void Ontimechange(DateTime now)            {                       updateText(now);        }             private void button1_Click(object sender, EventArgs e)            {                       libTime.stop();                 this.Close();           }             private void updateText(DateTime now)          {                    if (this.InvokeRequired)                   {                            this.Invoke(new UpdateTextHandler(updateText), new object[] { now });                             }                   else                {                       lbTime.Text = libTime.now.ToLongTimeString();                }              }      }}


 

 


另一个自己添加的类库中的TimeLab类中的内容:

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace timeLib{    public delegate void OnTimerChangeHandler(DateTime now);     public class timerLib      {                System.Threading.Timer m_timer;             public DateTime now;            public event OnTimerChangeHandler OnTimeChange;           public timerLib()              {                    }              private void timer_run(object o)         {                    now = DateTime.Now;                  if (OnTimeChange != null)                     {                            OnTimeChange(now); //OnTimeChange事件触发                  }               }             public void start()            {                  TimerCallback t_back = new TimerCallback(timer_run);//委托定义                  m_timer = new Timer(t_back, null, 1000, 1000);                //callback                       }               public void stop()                  {                        m_timer.Dispose();                     m_timer = null;             }      }}



其中System.Thread;中的Timer定时器的使用。http://kb.cnblogs.com/page/42532/Timer类:设置一个定时器,定时执行用户指定的函数。

定时器启动后,系统将自动建立一个新的线程,执行用户指定的函数初始化一个Timer对象: Timer timer = new Timer(timerDelegate, s,1000, 1000); // 第一个参数:指定了TimerCallback 委托,表示要执行的方法;// 第二个参数:一个包含回调方法要使用的信息的对象,或者为空引用;// 第三个参数:延迟时间——计时开始的时刻距现在的时间,单位是毫秒,指定为“0”表示立即启动计时器;// 第四个参数:定时器的时间间隔——计时开始以后,每隔这么长的一段时间,TimerCallback所代表的方法将被调用一次,单位也是毫秒。指定 Timeout.Infinite 可以禁用定期终止。Timer.Change()方法:修改定时器的设置。(这是一个参数类型重载的方法)
原创粉丝点击