是否有可能杀死WaitForSingleObject的(手柄,INFINITE)? [英] Is it possible to kill WaitForSingleObject(handle, INFINITE)?

查看:273
本文介绍了是否有可能杀死WaitForSingleObject的(手柄,INFINITE)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关使用WaitForSingleObject的应用程序()具有无限timout问题。

I am having problems closing an application that uses WaitForSingleObject() with an INFINITE timout.

全貌是这样的。我做下面让我的应用程序处理设备唤醒事件:

The full picture is this. I am doing the following to allow my application to handle the device wakeup event:

注册与事件:

CeRunAppAtEvent("\\\\.\\Notifications\\NamedEvents\\WakeupEvent",
    NOTIFICATION_EVENT_WAKEUP);



开始一个新的线程来等待:

Start a new thread to wait on:

Thread waitForWakeThread = new Thread(new ThreadStart(WaitForWakeup));
waitForWakeThread.Start();



然后做目标的方法如下:

Then do the following in the target method:

private void WaitForWakeup()
{
    IntPtr handle = CreateEvent(IntPtr.Zero, 0, 0, "WakeupEvent");
    while (true)
    {
        WaitForSingleObject(handle, INFINITE);
        MessageBox.Show("Wakey wakey");
    }
}

这一切,直到我试图关闭应用程序工作正常时,可以预见,WaitForSingleObject的继续等待并不允许应用程序正确关闭。我们只允许我们的应用程序的一个实例同时运行,我们检查此启动时。这似乎继续运行,直到设备软复位。

This all works fine until I try to close the application when, predictably, WaitForSingleObject continues to wait and does not allow the app to close properly. We only allow one instance of our app to run at a time and we check for this on startup. It appears to continue running until the device is soft reset.

有没有办法杀死WaitForSingleObject的等待,迫使它返回的句柄?

Is there a way to kill the handle that WaitForSingleObject is waiting for, to force it to return?

非常感谢。

推荐答案

使用WaitForMultipleObject代替,并通过2手柄。现有之一,而一个事件被称为像退出。在应用程序关闭,SetEvent的就退出事件,以及WaitForMultipleObject将返回,你可以得到它正常退出线程。

Use WaitForMultipleObject instead, and pass 2 handles. The existing one, and one for an event called something like 'exit'. During app shutdown, SetEvent on the exit event, and the WaitForMultipleObject will return and you can get it to exit the thread gracefully.

您需要在返回值切换WaitForMultipleObject到这取决于其中一个手柄被触发做适当的行为。

You need to switch on the return value of WaitForMultipleObject to do the appropriate behaviour depending on which one of the handles was triggered.

可能的话,还可以设置线程是一个后台线程。这将防止从关闭时,主线程终止停止你的应用程序

Possibly, also, you can set the thread to be a background thread. This will prevent it from stopping your application from shutting down when the main thread terminates.

请参阅:

< A HREF =http://msdn.microsoft.com/en-us/library/system.threading.thread.isbackground.aspx> http://msdn.microsoft.com/en-us/library/system.threading .thread.isbackground.aspx

这篇关于是否有可能杀死WaitForSingleObject的(手柄,INFINITE)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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