C# 三种计时器的使用
private delegate void Msg();
private Timer timer_1 = new Timer();
private System.Timers.Timer timer_2=new System.Timers.Timer();
private System.Threading.Timer timer_3;
private void Form1_Load(object sender, EventArgs e)
{
timer_1.Interval = 1;
timer_1.Start();
timer_1.Tick += new EventHandler(timer_1_Tick);
timer_2.AutoReset = true;
timer_2.Interval = 1;
timer_2.Elapsed += new System.Timers.ElapsedEventHandler(timer_2_Elapsed);
timer_2.Start();
timer_3= new System.Threading.Timer(new System.Threading.TimerCallback(GetData),"time",0,1);
}
void timer_2_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
this.Invoke( new Msg(()=>{
textBox2.Text = System.DateTime.Now.ToLocalTime().ToString();
}));
}
void timer_1_Tick(object sender, EventArgs e)
{
textBox1.Text = System.DateTime.Now.ToLocalTime().ToString();
}
private void button1_Click(object sender, EventArgs e)
{
timer_1.Stop();
timer_2.Stop();
}
private void button2_Click(object sender, EventArgs e)
{
timer_1.Start();
timer_2.Start();
}
private void GetData(object obj)
{
this.Invoke(new Msg(() =>
{
textBox3.Text = System.DateTime.Now.ToLocalTime().ToString();
}));
}