关于C#中timer类  在C#里关于定时器类就有3个   
1.定义在System.Windows.Forms里   
2.定义在System.Threading.Timer类里   
3.定义在System.Timers.Timer类里 
System.Windows.Forms.Timer是应用于WinForm中的,它是通过Windows消息机制实现的,类似于VB或Delphi中的Timer控件,内部使用API  SetTimer实现的。它的主要缺点是计时不精确,而且必须有消息循环,Console  Application(控制台应用程序)无法使用。   
  
System.Timers.Timer和System.Threading.Timer非常类似,它们是通过.NET  Thread  Pool实现的,轻量,计时精确,对应用程序、消息没有特别的要求。System.Timers.Timer还可以应用于WinForm,完全取代上面的Timer控件。它们的缺点是不支持直接的拖放,需要手工编码。
例: 
使用System.Timers.Timer类 
System.Timers.Timer t = new System.Timers.Timer(10000);//实例化Timer类,设置间隔时间为10000毫秒; 
t.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件; 
t.AutoReset = true;//设置是执行一次(false)还是一直执行(true); 
t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;
public void theout(object source, System.Timers.ElapsedEventArgs e) 

MessageBox.Show("OK!"); 
}  

解决方案 »

  1.   

    三种Timer
      

  2.   

    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;namespace WindowsFormsApplication4
    {
        public partial class Form1 : Form
        {
            int a, b,i;
            public Form1()
            {
                InitializeComponent();
                a = 0; b = 0; i = 60;
                textBox1.Text = a.ToString();
                textBox2.Text = b.ToString();
            }        private void timer1_Tick(object sender, EventArgs e)
            {
                textBox1.Text = a.ToString();
                textBox2.Text = b.ToString();
                if (a == 0 && b == 0)
                {
                    timer1.Enabled = false;
                    textBox1.ReadOnly = false;
                    textBox2.ReadOnly = false;
                    MessageBox.Show("Time Up!");
                    return;
                }
                if (b == 0)
                {
                    a = a-1;
                }
                if (a == 0 && b <= 3)
                {
                    System.Media.SoundPlayer player=new System.Media.SoundPlayer();
                    player.SoundLocation = "C:\\Windows\\Media\\Windows Error.wav";
                   // player.SoundLocation = ".\\Windows Error.wav";
                    player.Play();
                }
                if (i == 0)
                    i = 60;
                b = --i;
               
            }        private void OpenFileDialog(string p, string p_2)
            {
                throw new NotImplementedException();
            }        private void button1_Click(object sender, EventArgs e)
            {
                
                string str1 = textBox1.Text;
                string str2 = textBox2.Text;
                if (int.TryParse(str1, out a) == false)
                {
                    MessageBox.Show("第一个数据不是合法的整数");
                    return;
                }
                if (!int.TryParse(str2, out b))
                {
                    MessageBox.Show("第二个数据不是合法的整数");
                    return;
                }
                i = b;
                textBox1.ReadOnly = true;
                textBox2.ReadOnly = true;
                textBox1.BackColor = Color.FromArgb(255, 255, 255);
                textBox2.BackColor = Color.FromArgb(255, 255, 255);
                timer1.Enabled = true;
            }        private void timer2_Tick(object sender, EventArgs e)
            {
                string text = textBox3.Text;
                string text1 = text.Substring(1);
                string text2 = text1 + text[0];
                textBox3.Text = text2;
            }        private void timer3_Tick(object sender, EventArgs e)
            {
               // string s1 = DateTime.Now.ToString();
                string s = DateTime.Now.ToLongTimeString();
                textBox4.Text = s;
            }        private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
                if (checkBox1.Checked == false)
                    this.TopMost = false;
                else
                    this.TopMost = true;
            }        private void button2_Click(object sender, EventArgs e)
            {
                timer1.Enabled = false;
            }        private void checkBox2_CheckedChanged(object sender, EventArgs e)
            {
                if (checkBox2.Checked)
                {
                    textBox1.Text = "0";
                    textBox2.Text = "0";
                    timer1.Enabled = false;
                }
            }
        }
    }
      

  3.   

            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);            System.Threading.Thread t = new System.Threading.Thread(delegate()
                {
                    while (true)
                    {
                        System.Diagnostics.Debug.WriteLine(System.DateTime.Now.ToString());
                        System.Threading.Thread.Sleep(1000);
                    }
                }
                );            t.Start();
                t.Join();
            }