Monitor.Wait()坏了吗? [英] Monitor.Wait() broken?

查看:91
本文介绍了Monitor.Wait()坏了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在从控制台事件处理程序调用Monitor.Wait()。

它不起作用 - 调用Wait()会立即导致

退出流程。由于某种原因,从事件处理程序调用Wait()是不可能的吗?


附上小代码示例。


谢谢,


Michi。


使用System;

使用System.Runtime.InteropServices;

使用System.Threading;


class TestClass

{

private static bool finished = false;


私有委托Boolean EventHandler(int sig);


private static Boolean Handler(int sig)

{

Monitor.Enter(typeof(TestClass));

while(!finished)

{

控制台。 WriteLine(调用Wait());

Monitor.Wait(typeof(TestClass));

Console.WriteLine(" Wait()return") ;

}

Monitor.Exit(typeof(TestClass));

返回true;

}


私有静态EventHandler _handler = new

EventHandler(处理程序);


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

}

}

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

推荐答案

我只是尝试从事件内部调用Thread.Sleep()

处理程序。

这与调用Monitor.Wait()完全相同 -

立即进程退出。

调用Thread.Suspend()也有同样的效果。


什么是在控制台事件中使用这些线程异常

处理程序?


干杯,


Michi。
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.


Michi Henning< an ******* @ discussion.mic rosoft.com>写道:
Michi Henning <an*******@discussions.microsoft.com> wrote:
我正在从控制台事件处理程序调用Monitor.Wait()。
它不起作用 - 对Wait()的调用会立即导致
退出的过程。某些原因是否无法从事件处理程序调用Wait()?

附上小代码示例。
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.




谢谢对于代码 - 您使用的操作系统和框架版本是什么?我的

笔记本电脑上有XP Pro和v1.1,它似乎工作正常。 (我认为

的想法是运行它并且相当快地按ctrl + c?)


-

Jon Skeet - < sk *** @ pobox.com>
http:// www .pobox.com / ~siget

如果回复小组,请不要给我发邮件



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


>
感谢您的代码 - 您使用的是什么操作系统和框架版本是
? Mylaptop上有XP专业版和v1.1版本,好像可以用到b $ b。 (我认为这个想法是运行它并且很快按ctrl + c?)
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?)




我正在使用Win XP Pro,版本5.1.2600。代码是使用Visual C#.NET 2003,版本7.1.3088编译的


框架版本是1.1.4322。


是的,想法是在
流程启动的三秒内按Ctrl-C。然后处理程序应该输入等待

并在主线程从其

睡眠返回时被唤醒。我正在尝试构建这整个机器,所以我可以在收到控制台时获得
控制进程关闭

事件...当我运行代码并按下Ctrl时-C,过程

以状态130退出。最后一行是打印机

是Calling Wait()。


如果我用Suspend()或Sleep()替换Wait()调用

,就会发生同样的事情。


干杯,


Michi。



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.


这篇关于Monitor.Wait()坏了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆