[英]Wait for a message in a NativeWindow
What I am trying to do is catch and wait for a WM_TIMER
message on a window within my process (though one which I have no control of). 我想要做的是在进程中的窗口上捕获并等待
WM_TIMER
消息(尽管我无法控制该消息)。
I am trying to use an AutoResetEvent
in order to wait for the message. 我正在尝试使用
AutoResetEvent
来等待消息。
The WaitForMaterialUpdate
method connects the NativeWindow
to the window, and blocks until it receives a message. WaitForMaterialUpdate
方法将NativeWindow
连接到窗口,并阻塞直到接收到消息。
This is the code I am using: 这是我正在使用的代码:
public class MaterialEditorWindow : NativeWindow
{
private const int WM_TIMER = 0x0113;
private AutoResetEvent waiter;
public void WaitForMaterialUpdate(IntPtr handle)
{
waiter = new AutoResetEvent(false);
AssignHandle(handle);
waiter.WaitOne(5000);
ReleaseHandle();
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_TIMER) waiter.Set();
base.WndProc(ref m);
}
}
I am not in a very debuggable environment, but I have confirmed using MessageBox
that the window is in fact receiving WM_TIMER
messages during the wait period, yet WaitOne
always waits the full 5000 ms timeout period before returning. 我不是处于非常可调试的环境中,但是我已经使用
MessageBox
确认了该窗口实际上在等待期内接收到WM_TIMER
消息,但是WaitOne
始终等待完整的5000 ms超时周期后才返回。
Any idea where I'm going wrong? 知道我哪里出错了吗?
WaitOne()
is a blocking call. WaitOne()
是一个阻止调用。
The UI thread will not receive any messages until WaitOne()
returns. UI线程将不会收到任何消息,直到
WaitOne()
返回。 Since you set the wait handle when the UI thread receives a message, you have a deadlock. 由于您在UI线程收到消息时设置了等待句柄,因此您将出现死锁。
You need to do this on a background thread, or simply call a callback when you receive the message. 您需要在后台线程上执行此操作,或者仅在收到消息时调用回调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.