S
solandre
I have encountered some very puzzeling behavior of .NET which I'm
seeking an explanation for:
This is just a test program to check out how
ThreadPool.RegisterWaitForSingelObject works.
The following code ....
class Program
{
static int NumRegisters=2;
static RegisteredWaitHandle[] rwh = new
RegisteredWaitHandle[Program.NumRegisters];
static void Main(string[] args)
{
AutoResetEvent wh = new AutoResetEvent(false);
for (int i = 0; i < Program.NumRegisters; i++)
{
Program.rwh =
ThreadPool.RegisterWaitForSingleObject(wh, Program.Do, i, 2000, true);
}
wh.Set();
wh.Set();
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
static void Do(object o, bool TimedOut)
{
Console.Write("Register {0}: ",(int)o);
if (TimedOut)
{
Console.WriteLine("Timeout while waiting for
signal.");
}
else
{
Console.WriteLine("Procedure started. Doing something
and exiting.");
//Thread.Sleep(1000);
}
}
}
yields this output:
Register 0: Procedure started. Doing something and exiting.
Press any key to exit.
Register 1: Timeout while waiting for signal.
Very intersting, as there is sent a signal two times, but just one
registered procedure is started.
If I insert a Thread.Sleep(0); between the two signals .... (the
following code is the same as above, just with one additional line)
class Program
{
static int NumRegisters=2;
static RegisteredWaitHandle[] rwh = new
RegisteredWaitHandle[Program.NumRegisters];
static void Main(string[] args)
{
AutoResetEvent wh = new AutoResetEvent(false);
for (int i = 0; i < Program.NumRegisters; i++)
{
Program.rwh =
ThreadPool.RegisterWaitForSingleObject(wh, Program.Do, i, 2000, true);
}
wh.Set();
Thread.Sleep(0);
wh.Set();
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
static void Do(object o, bool TimedOut)
{
Console.Write("Register {0}: ",(int)o);
if (TimedOut)
{
Console.WriteLine("Timeout while waiting for
signal.");
}
else
{
Console.WriteLine("Procedure started. Doing something
and exiting.");
//Thread.Sleep(1000);
}
}
}
it works as expected:
Register 0: Procedure started. Doing something and exiting.
Register 1: Procedure started. Doing something and exiting.
Press any key to exit.
Anyone who can explain why?
seeking an explanation for:
This is just a test program to check out how
ThreadPool.RegisterWaitForSingelObject works.
The following code ....
class Program
{
static int NumRegisters=2;
static RegisteredWaitHandle[] rwh = new
RegisteredWaitHandle[Program.NumRegisters];
static void Main(string[] args)
{
AutoResetEvent wh = new AutoResetEvent(false);
for (int i = 0; i < Program.NumRegisters; i++)
{
Program.rwh =
ThreadPool.RegisterWaitForSingleObject(wh, Program.Do, i, 2000, true);
}
wh.Set();
wh.Set();
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
static void Do(object o, bool TimedOut)
{
Console.Write("Register {0}: ",(int)o);
if (TimedOut)
{
Console.WriteLine("Timeout while waiting for
signal.");
}
else
{
Console.WriteLine("Procedure started. Doing something
and exiting.");
//Thread.Sleep(1000);
}
}
}
yields this output:
Register 0: Procedure started. Doing something and exiting.
Press any key to exit.
Register 1: Timeout while waiting for signal.
Very intersting, as there is sent a signal two times, but just one
registered procedure is started.
If I insert a Thread.Sleep(0); between the two signals .... (the
following code is the same as above, just with one additional line)
class Program
{
static int NumRegisters=2;
static RegisteredWaitHandle[] rwh = new
RegisteredWaitHandle[Program.NumRegisters];
static void Main(string[] args)
{
AutoResetEvent wh = new AutoResetEvent(false);
for (int i = 0; i < Program.NumRegisters; i++)
{
Program.rwh =
ThreadPool.RegisterWaitForSingleObject(wh, Program.Do, i, 2000, true);
}
wh.Set();
Thread.Sleep(0);
wh.Set();
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
static void Do(object o, bool TimedOut)
{
Console.Write("Register {0}: ",(int)o);
if (TimedOut)
{
Console.WriteLine("Timeout while waiting for
signal.");
}
else
{
Console.WriteLine("Procedure started. Doing something
and exiting.");
//Thread.Sleep(1000);
}
}
}
it works as expected:
Register 0: Procedure started. Doing something and exiting.
Register 1: Procedure started. Doing something and exiting.
Press any key to exit.
Anyone who can explain why?