问题如题,有哪位兄弟知道?帮帮忙~

解决方案 »

  1.   

    [C#]
    public Timer(
       TimerCallback callback,
       object state,
       int dueTime,
       int period
    );参数
    callback 
    一个 TimerCallback 委托,表示要执行的方法。 
    state 
    一个包含回调方法要使用的信息的对象,或者为空引用(Visual Basic 中为 Nothing)。 
    dueTime 
    调用 callback 之前延迟的时间量(以毫秒为单位)。指定 Timeout.Infinite 以防止计时器开始计时。指定零 (0) 以立即启动计时器。 
    period 
    调用 callback 的时间间隔(以毫秒为单位)。指定 Timeout.Infinite 可以禁用定期终止。备注
    在超过 dueTime 以后及此后每隔 period 时间间隔,都会调用一次 callback 参数所指定的委托。[Visual Basic] 注意   Visual Basic 用户可以省略 TimerCallback 构造函数,在指定回调方法时只需使用 AddressOf 运算符即可。Visual Basic 将自动调用正确的委托构造函数。
    如果 dueTime 为零 (0),则立即调用 callback。如果 dueTime 为 Timeout.Infinite,则不会调用 callback;计时器将被禁用,但通过调用 Change 方法可以重新启用计时器。如果 period 为零 (0) 或 Infinite,并且 dueTime 不是 Infinite,则只会调用一次 callback;计时器的定期行为将被禁用,但使用 Change 方法可以重新启用该行为。
      

  2.   

    下面的示例说明 Timer 类的功能。[Visual Basic] 
    Imports System
    Imports System.ThreadingClass TimerExampleState
       Public counter As Integer = 0
       Public tmr As Timer
    End Class 'TimerExampleStateClass App
       
       Public Shared Sub Main()
          Dim s As New TimerExampleState()
          
          ' Create the delegate that invokes methods for the timer.
          Dim timerDelegate As New TimerCallback(AddressOf CheckStatus)
          
          ' Create a timer that waits one second, then invokes every second.
          Dim timer As New Timer(timerDelegate, s, 1000, 1000)
          
          ' Keep a handle to the timer, so it can be disposed.
          s.tmr = timer
          
          ' The main thread does nothing until the timer is disposed.
          While Not (s.tmr Is Nothing)
             Thread.Sleep(0)
          End While
          Console.WriteLine("Timer example done.")
       End Sub 'Main
       
       ' The following method is called by the timer's delegate.
       Shared Sub CheckStatus(state As [Object])
          Dim s As TimerExampleState = CType(state, TimerExampleState)
          s.counter += 1
          Console.WriteLine("{0} Checking Status {1}.", DateTime.Now.TimeOfDay, s.counter)
          If s.counter = 5 Then
             ' Shorten the period. Wait 10 seconds to restart the timer.
             s.tmr.Change(10000, 100)
             Console.WriteLine("changed...")
          End If
          If s.counter = 10 Then
             Console.WriteLine("disposing of timer...")
             s.tmr.Dispose()
             s.tmr = Nothing
          End If
       End Sub 'CheckStatus
    End Class 'App[C#] 
    using System;
    using System.Threading;class TimerExampleState 
    {
       public int counter = 0;
       public Timer tmr;
    }class App 
    {
       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.
       Timer timer = new Timer(timerDelegate, s,1000, 1000);
        
       // Keep a handle to the timer, so it can be disposed.
       s.tmr = timer;   // The main thread does nothing until the timer is disposed.
       while(s.tmr != null)
             Thread.Sleep(0);
       Console.WriteLine("Timer example done.");
       }
       // The following method is called by the timer's delegate.   static void CheckStatus(Object state)
       {
       TimerExampleState s =(TimerExampleState)state;
       s.counter++;
       Console.WriteLine("{0} Checking Status {1}.",DateTime.Now.TimeOfDay, s.counter);
       if(s.counter == 5)
          {
          // Shorten the period. Wait 10 seconds to restart the timer.
          (s.tmr).Change(10000,100);
          Console.WriteLine("changed...");
          }
       if(s.counter == 10)
          {
          Console.WriteLine("disposing of timer...");
          s.tmr.Dispose();
          s.tmr = null;
          }
       }
    }[C++] 
    #using <mscorlib.dll>using namespace System;
    using namespace System::Threading;__gc class TimerExampleState 
    {
    public:
        int counter;
    public:
        Timer* tmr;
    };__gc class StatusChecker
    {
    public:
        // The following method is called by the timer's delegate.
        static void CheckStatus(Object* state) 
        {
            TimerExampleState* s =dynamic_cast<TimerExampleState*>(state);
            s->counter++;
            Console::WriteLine(S" {0} Checking Status {1}.", __box(DateTime::Now.TimeOfDay), __box(s->counter));
            if (s->counter == 5)
            {
                // Shorten the period. Wait 10 seconds to restart the timer.
                s->tmr->Change(10000, 100);
                Console::WriteLine(S"changed...");
            }
            if (s->counter == 10) 
            {
                Console::WriteLine(S"disposing of timer...");
                s->tmr->Dispose();
                s->tmr = 0;
            }
        }
    };void main() 
    {
        TimerExampleState* s = new TimerExampleState();    // Create the delegate that invokes methods for the timer.
        TimerCallback* timerDelegate = new TimerCallback(0, StatusChecker::CheckStatus);    // Create a timer that waits one second, then invokes every second.
        Timer* timer = new Timer(timerDelegate, s, 1000, 1000);    // Keep a handle to the timer, so it can be disposed.
        s->tmr = timer;    // The main thread does nothing until the timer is disposed.
        while(s->tmr != 0)
            Thread::Sleep(0);
        Console::WriteLine(S"Timer example done.");
    }[Visual Basic, C#, C++] 此代码产生下面的输出(返回的确切执行时间会有所不同):08:02:09.4811456 Checking Status 1. 
        08:02:10.4825856 Checking Status 2. 
        08:02:11.4840256 Checking Status 3. 
        08:02:12.4854656 Checking Status 4. 
        08:02:13.4869056 Checking Status 5. 
        changed... 
        08:02:23.4912912 Checking Status 6. 
        08:02:23.5914352 Checking Status 7. 
        08:02:23.6915792 Checking Status 8. 
        08:02:23.7917232 Checking Status 9. 
        08:02:23.8918672 Checking Status 10. 
        disposing of timer... 
        Timer example done.