Monitor.Wait() broken?

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));
}
}
 
G

Guest

I just tried calling Thread.Sleep() from inside the event
handler.
That does the same thing as calling Monitor.Wait() --
immediate process exit.
And calling Thread.Suspend() has the same effect as well.

What's with these threading anomalies in a console event
handler?

Cheers,

Michi.
 
J

Jon Skeet [C# MVP]

Michi Henning said:
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 for the code - what OS and framework version are you using? My
laptop has XP Pro and v1.1 on it, and it seems to work fine. (I presume
the idea is to run it and fairly quickly press ctrl+c?)
 
M

Michi Henning

Thanks for the code - what OS and framework version are you using? My
laptop has XP Pro and v1.1 on it, and it seems to work fine. (I presume
the idea is to run it and fairly quickly press ctrl+c?)

I'm using Win XP Pro, version 5.1.2600. The code is
compiled with Visual C# .NET 2003, version 7.1.3088.
Framework version is 1.1.4322.

Yes, the idea is to hit Ctrl-C within three seconds of
process start-up. The handler should then enter the wait
and get woken up when the main thread returns from its
sleep. I'm trying to build this entire machinery so I can
get controlled process shutdown on receipt of a console
event... When I run the code and hit Ctrl-C, the process
exits with status 130. The last line that is printer
is "Calling Wait()".

The exact same thing happens if I replace the Wait() call
with Suspend() or Sleep().

Cheers,

Michi.
 
J

Jon Skeet [C# MVP]

Michi Henning said:
I'm using Win XP Pro, version 5.1.2600. The code is
compiled with Visual C# .NET 2003, version 7.1.3088.
Framework version is 1.1.4322.

Yes, the idea is to hit Ctrl-C within three seconds of
process start-up. The handler should then enter the wait
and get woken up when the main thread returns from its
sleep. I'm trying to build this entire machinery so I can
get controlled process shutdown on receipt of a console
event... When I run the code and hit Ctrl-C, the process
exits with status 130. The last line that is printer
is "Calling Wait()".

The exact same thing happens if I replace the Wait() call
with Suspend() or Sleep().

Very odd... as I say, it's working fine on my box - I get:
calling Wait()
Wait() returned

as you'd expect.

Anyone else care to try this to work out what's going on?

Michi, I've just thought - I've been compiling and running the code
from the command line rather than from Visual Studio.NET - have you
tried that?
 
W

Willy Denoyette [MVP]

Works for me as expected on XP prof and home, W2K and W2K3.
Compiled (both debug and release) with commandline compiler (csc.exe ) and
framework 1.1.

Willy.
 
M

Michi Henning

().
Very odd... as I say, it's working fine on my box - I get:
calling Wait()
Wait() returned

as you'd expect.
Right.

Michi, I've just thought - I've been compiling and running the code
from the command line rather than from Visual Studio.NET - have you
tried that?

I've been compiling the code with Visual Studio and
running it from the command line (in a cygwin window).
I'll try compiling from the command line and also running
it in a normal console window. I'll report back after the
Xmas period -- thanks for you help with this John!

Cheers,

Michi.
 
M

Michi Henning

Works for me as expected on XP prof and home, W2K and W2K3.
Compiled (both debug and release) with commandline compiler (csc.exe ) and
framework 1.1.

Wow Willy, you certainly tried hard! :) Thanks for that --
at least I now know that I still know how to write code :-
)

I'm beginning to suspect that cygwin might have something
to do with this. I'll verify after Xmas.

Cheers,

Michi.
 
M

Michi Henning

Jon Skeet [C# MVP] wrote:

Very odd... as I say, it's working fine on my box - I get:
calling Wait()
Wait() returned

as you'd expect.

Anyone else care to try this to work out what's going on?

Michi, I've just thought - I've been compiling and running the code
from the command line rather than from Visual Studio.NET - have you
tried that?

OK, just tried this again, this time from a console window instead of
cygwin. Works fine from a console window, but bombs out under cygwin.

Anyone have any idea what might be going on there? I suspect that it
has to do with an interrupted system call that isn't being restarted,
but I don't know enough about cygwin to be sure...

Cheers,

Michi.
 
J

Jon Skeet [C# MVP]

Michi Henning said:
Anyone have any idea what might be going on there? I suspect that it
has to do with an interrupted system call that isn't being restarted,
but I don't know enough about cygwin to be sure...

Might it be that cygwin adds its own ctrl-c handler which kills the
process itself?
 
M

Michi Henning

Jon said:
Might it be that cygwin adds its own ctrl-c handler which kills the
process itself?

After browsing the cygwin mailing list archives, it appears that signal
handling in cygwin isn't quite correct. I've sent a query to the cygwin
people. Hopefully they'll be able to help. At any rate, this is
definitely a cygwin problem -- I'm seeing the same behavior with a VC++
binary that calls SetConsoleCtrlHandler() (the process exits once the
handler is called).

Sorry to have bothered you here with this -- wasn't anything to do with
C# after all...

Cheers,

Michi.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top