实现在用户定义的时间间隔引发事件的计时器。此计时器最宜用于 Windows 窗体应用程序中,并且必须在窗口中使用。

解决方案 »

  1.   

    类库中可以用System.Threading.Timer
      

  2.   


    using System.ComponentModel;
    using System.Windows.Forms;
      

  3.   

    整个例子给你看看,看看对你是否有帮助。另外楼主是否昨晚没睡觉啊,4:30就发贴了:)using System;
    using System.Threading;namespace TestThreadPool
    {
    class TimerExampleState
    {
    public int counter;
    public Timer tmr;
    }
    class App
    {
    private static int _counter=0;
    private static Timer _tmr; public static void Main()
    {
    //TimerExampleState s=new TimerExampleState(); // Create the delegate that invokes methods for the timer.
    TimerCallback timerDelegate=new TimerCallback(CheckStatus);

    // Create a timer that waits one second, then invokes every second.
    _tmr=new Timer(timerDelegate,null,1000,1000); // Keep a handle to the timer, so it can be disposed.
    //s.tmr=timer;
    while(_tmr!=null)
    Thread.Sleep(0);
    Console.WriteLine("Timer example done.");
    Console.ReadLine();
    }
    static void CheckStatus(object state)
    {
    //TimerExampleState s=(TimerExampleState)state;
    _counter++;
    Console.WriteLine("{0} Checking Status {1}",DateTime.Now.TimeOfDay,_counter);
    if(_counter==5)
    {
    _tmr.Change(10000,100);
    Console.WriteLine("Changed...");
    }
    if(_counter==10)
    {
    Console.WriteLine("Disposed");
    _tmr.Dispose();
    _tmr=null;
    }
    }
    }
    }
      

  4.   

    添加引用Windows.Forms.Dll。在非Windows Forms程序中,最好使用Windows.Timers.Timer和Windows.Threading.Timer,不要用Windows.Forms.Timer