[Visual Basic]
Imports System
Imports System.Threading
Class TimerExampleState
Public counter As Integer = 0
Public tmr As Timer
End Class 'TimerExampleState
Class 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.");
}
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.