M
Michi Henning
Hi,
I'm calling Monitor.Wait() from a console event handler.
It's not working -- the call to Wait() immediately causes
the process to exit. Is it impossible for some reason to
call Wait() from an event handler?
Small code example attached.
Thanks,
Michi.
using System;
using System.Runtime.InteropServices;
using System.Threading;
class TestClass
{
private static bool finished = false;
private delegate Boolean EventHandler(int sig);
private static Boolean Handler(int sig)
{
Monitor.Enter(typeof(TestClass));
while(!finished)
{
Console.WriteLine("calling Wait()");
Monitor.Wait(typeof(TestClass));
Console.WriteLine("Wait() returned");
}
Monitor.Exit(typeof(TestClass));
return true;
}
private static EventHandler _handler = new
EventHandler(Handler);
[DllImport("Kernel32")]
private static extern Boolean SetConsoleCtrlHandler
EventHandler handler, Boolean add);
static void Main()
{
SetConsoleCtrlHandler(_handler, true);
Thread.Sleep(3000);
Monitor.Enter(typeof(TestClass));
finished = true;
Monitor.Pulse(typeof(TestClass));
Monitor.Exit(typeof(TestClass));
}
}
I'm calling Monitor.Wait() from a console event handler.
It's not working -- the call to Wait() immediately causes
the process to exit. Is it impossible for some reason to
call Wait() from an event handler?
Small code example attached.
Thanks,
Michi.
using System;
using System.Runtime.InteropServices;
using System.Threading;
class TestClass
{
private static bool finished = false;
private delegate Boolean EventHandler(int sig);
private static Boolean Handler(int sig)
{
Monitor.Enter(typeof(TestClass));
while(!finished)
{
Console.WriteLine("calling Wait()");
Monitor.Wait(typeof(TestClass));
Console.WriteLine("Wait() returned");
}
Monitor.Exit(typeof(TestClass));
return true;
}
private static EventHandler _handler = new
EventHandler(Handler);
[DllImport("Kernel32")]
private static extern Boolean SetConsoleCtrlHandler
EventHandler handler, Boolean add);
static void Main()
{
SetConsoleCtrlHandler(_handler, true);
Thread.Sleep(3000);
Monitor.Enter(typeof(TestClass));
finished = true;
Monitor.Pulse(typeof(TestClass));
Monitor.Exit(typeof(TestClass));
}
}