I have a bit of an issue with an interaction between two applications.
You can replicate the issue with code like this:
using (Form1 frm = new Form1())
{
frm.ShowDialog();
}
Thread.Sleep(120000);
Using this code, a separate application, which we do not control, freezes when our application is running the Thread.Sleep
portion of the code. We actually have a good idea of what's happening, having been on the receiving end of this bug before: the other app has a callback from the native EnumWindows
method which doesn't check to see if windows are visible using the IsWindowVisible native method. Then when it calls GetWindowText
or possible some other function, it hangs.
The reason for this hang is that it is trying to talk to our (now closed) window, which still has a handle registered, but doesn't respond to messages sent to its queue, and so the caller just sits there indefinitely (until the window fully dies after our Thread.Sleep).
Here is some sample code for the "untouchable" application that we have to deal with:
public bool RunTest()
{
return NativeMethods.EnumWindows(new NativeMethods.EnumWindowsProc(EnumWindowsCallback), IntPtr.Zero);
}
private bool EnumWindowsCallback(IntPtr windowHandle, IntPtr parameter)
{
NativeMethods.SendMessage(windowHandle, 0x000E, IntPtr.Zero, IntPtr.Zero);
return true;
}
If you run the first code, close the window that it opens, then run the second code block in a separate process, the second (EnumWindows) process will hang.
My problem is that I have to deal with this, and can't just fix the code in the hanging application. I can kick the window off into a separate thread, but that seems clumsy and somewhat fragile. Does anyone know of a way to clean this problem up using native calls, or any other more direct method?