Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,45 @@
using namespace System;
using namespace System::Threading;

public ref class Example
ref class TimerState
{
public:
int counter;
};

ref class Example
{
private:
static Timer^ ticker;
static Timer^ timer;

public:
static void TimerMethod(Object^ state)
{
Console::Write(".");
}
static void TimerTask(Object^ state)
{
Console::WriteLine("{0:HH:mm:ss.fff}: starting a new callback.", DateTime::Now);

TimerState^ timerState = dynamic_cast<TimerState^>(state);
Interlocked::Increment(timerState->counter);
}

static void Main()
{
TimerCallback^ tcb =
gcnew TimerCallback(&TimerMethod);
static void Main()
{
TimerCallback^ tcb = gcnew TimerCallback(&TimerTask);
TimerState^ state = gcnew TimerState();
state->counter = 0;
timer = gcnew Timer(tcb, state, 1000, 2000);

ticker = gcnew Timer(tcb, nullptr, 1000, 1000);
while (state->counter <= 10)
{
Thread::Sleep(1000);
}

Console::WriteLine("Press the Enter key to end the program.");
Console::ReadLine();
}
timer->~Timer();
Console::WriteLine("{0:HH:mm:ss.fff}: done.", DateTime::Now);
}
};

int main()
{
Example::Main();
Example::Main();
}

// </snippet2>
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
// <snippet2>
using System;
using System.Threading;
using System.Threading.Tasks;

public class Example
class Program
{
private static Timer ticker;
private static Timer timer;

public static void TimerMethod(object state)
static void Main(string[] args)
{
Console.Write(".");
var timerState = new TimerState { Counter = 0 };

timer = new Timer(
callback: new TimerCallback(TimerTask),
state: timerState,
dueTime: 1000,
period: 2000);

while (timerState.Counter <= 10)
{
Task.Delay(1000).Wait();
}

timer.Dispose();
Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff}: done.");
}

public static void Main()
private static void TimerTask(object timerState)
{
ticker = new Timer(TimerMethod, null, 1000, 1000);
Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff}: starting a new callback.");
var state = timerState as TimerState;
Interlocked.Increment(ref state.Counter);
}

Console.WriteLine("Press the Enter key to end the program.");
Console.ReadLine();
class TimerState
{
public int Counter;
}
}
// </snippet2>
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
' <snippet2>
Imports System
Imports System.Threading

Public Class Example
Private Shared ticker As Timer
Module Program

Public Shared Sub TimerMethod(state As Object)
Console.Write(".")
Private Timer As Timer

Sub Main(args As String())

Dim StateObj As New TimerState
StateObj.Counter = 0

Timer = New Timer(New TimerCallback(AddressOf TimerTask), StateObj, 1000, 2000)

While StateObj.Counter <= 10
Task.Delay(1000).Wait()
End While

Timer.Dispose()
Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff}: done.")
End Sub

Public Shared Sub Main()
ticker = New Timer(AddressOf TimerMethod, Nothing, 1000, 1000)
Private Sub TimerTask(ByVal StateObj As Object)

Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff}: starting a new callback.")

Console.WriteLine("Press the Enter key to end the program.")
Console.ReadLine()
Dim State As TimerState = CType(StateObj, TimerState)
Interlocked.Increment(State.Counter)
End Sub
End Class

Private Class TimerState
Public Counter As Integer
End Class
End Module
' </snippet2>