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).
I am trying to use an AutoResetEvent
in order to wait for the message.
The WaitForMaterialUpdate
method connects the NativeWindow
to the window, and blocks until it receives a message.
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.
Any idea where I'm going wrong?
WaitOne()
is a blocking call.
The UI thread will not receive any messages until WaitOne()
returns. Since you set the wait handle when the UI thread receives a message, you have a deadlock.
You need to do this on a background thread, or simply call a callback when you receive the message.