473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Monitor.Wait() broken?

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

Nov 15 '05 #1
11 2903
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.
Nov 15 '05 #2
Michi Henning <an*******@discussions.microsoft.com> wrote:
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?)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
>
Thanks for the code - what OS and framework version are you using? Mylaptop has XP Pro and v1.1 on it, and it seems to work fine. (I presumethe 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.

Nov 15 '05 #4
Michi Henning <an*******@discussions.microsoft.com> wrote:
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().


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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
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.

"Michi Henning" <an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl...

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.

Nov 15 '05 #6
Hi,

Works fine for me, too.
latest XP, .net 1.1

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rhand.com

"Michi Henning" <an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl...

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.

Nov 15 '05 #7
().

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 codefrom the command line rather than from Visual Studio.NET - have youtried 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.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.

Nov 15 '05 #8
Works for me as expected on XP prof and home, W2K and W2K3.Compiled (both debug and release) with commandline compiler (csc.exe ) andframework 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.

Nov 15 '05 #9
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.

Nov 15 '05 #10
Michi Henning <mi***@zeroc.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #11
Jon Skeet [C# MVP] wrote:
Michi Henning <mi***@zeroc.com> wrote:
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?


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.

Nov 15 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: DT | last post by:
Hi All, I understand the Thread.Sleep method is a very effcient wait state. Can the same be said for the Monitor.Wait method? Many Thanks DT
0
by: Chris Chilvers | last post by:
I've found the docs to be rather scant on exactly what the second parameter to WaitHandle.WaitOne(Int32, Boolean) was for. The one named exitContext so I started playing with the synchronize...
12
by: Perecli Manole | last post by:
I am having some strange thread synchronization problems that require me to better understand the intricacies of Monitor.Wait/Pulse. I have 3 threads. Thread 1 does a Monitor.Wait in a SyncLock...
3
by: Adam Clauss | last post by:
Documentation states that a call to Wait releases the lock, and will not return until the lock is reacquired. How do exceptions figure into this? Are they considered a form of "return"? ...
1
by: MessyHeart | last post by:
The documentation on MSDN for the overloaded Monitor.wait(object,timespan, bool) is: public static bool Wait( Object obj, TimeSpan timeout, bool exitContext ) Releases the lock...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.