If you don't say much, look at the code:
Using System;
Using System. Collections. Generic;
Using System. The Text;
The namespace NET. The MST. Sixth. Reenter
{
The class Reenter
{
// static member used to cause thread synchronization problems
Private static int TestInt1 = 0;
Private static int TestInt2 = 0;
Private static object locko = new object();
The static void Main (string [] args)
{
Console.WriteLine(" system.timer. Timer callback method reentry test: ");
TimersTimerReenter ();
// here make sure that callback methods that have already started have a chance to end
System. Threading. Thread. Sleep (2 * 1000);
Console.writeline (" system.threading.timer callback method reentry test: ");
ThreadingTimerReenter ();
The Console. The Read ();
}
/ / / < summary >
/// shows the callback method reentrant of system.timer.timer
/ / / < / summary >
The static void TimersTimerReenter ()
{
Timer = new system.timer.timer ();
The timer. The Interval = 100; / / 100 milliseconds
The timer. Elapsed + = TimersTimerHandler;
The timer. The Start ();
System. Threading. Thread. Sleep (2 * 1000); // run for 2 seconds
The timer. Stop ();
}
/ / / < summary >
/// shows the callback method reentrancy of system.threading.timer
/ / / < / summary >
The static void ThreadingTimerReenter ()
{
/ / 100 milliseconds
Using (system.threading.timer Timer = new system.threading.timer
(the new System. Threading. TimerCallback (ThreadingTimerHandler), null, 0, 100))
{
System. Threading. Thread. Sleep (2 * 1000); // run for 2 seconds
}
}
/ / / < summary >
/// callback method of system.timer.timer
/ / / < / summary >
/ / / < param name = "sender" > < param >
/ / / < param name = "args" > < param >
Private static void TimersTimerHandler(object sender,EventArgs args)
{
The lock (locko)
{
Console.WriteLine(" test integer: "+ testint1.tostring ());
// sleep for 10 seconds to ensure reentry
System. Threading. Thread. Sleep (300);
TestInt1 + +;
Console.WriteLine(" test integer after incrementing 1: "+ testint1.tostring ());
}
}
/ / / < summary >
/// callback method for system.threading.timer
/ / / < / summary >
/ / / < param name = "state" > < param >
Private static void ThreadingTimerHandler(object state)
{
The lock (locko)
{
Console.WriteLine(" test integer: "+ testint2.tostring ());
// sleep for 10 seconds to ensure reentry
System. Threading. Thread. Sleep (300);
TestInt2 + +;
Console.WriteLine(" test integer after incrementing 1: "+ testint2.tostring ());
}
}
}
}