开发者

FindWindow doesn't return IntPtr.Zero, although the window is already closed

开发者 https://www.devze.com 2023-01-17 23:22 出处:网络
I\'m trying to control a Java application from my C# program. Before the program begins to input data, it checks if there are any pervious data input windows, and tries to close them.

I'm trying to control a Java application from my C# program. Before the program begins to input data, it checks if there are any pervious data input windows, and tries to close them.

The code for checking if a window exists is:

public static bool WindowExists(string WindowName)
{
    IntPtr hWnd = FindWindow(null, WindowName);
    return (hWnd != IntPtr.Zero);
}

Until now, I simply used this method in a while loop (sending Alt+F4 to the windows), until there was no open input window left.

A while ago the Java application got an update, and this method no longer works. For some reason, WindowExists keeps returning true, even after the data input window is closed. This only happens if the input window existed at least once.

I even tried to add Thread.Sleep(开发者_如何学运维) to the loop, to make sure this isn't caused by some latency in the Java app, but it didn't work.

Sometimes simply calling WindowExists crashes the input window.

There's no problem with the code, because it has worked before, and it still works with other programs, so it must be a problem with the update in the Java program.

Is there any alternative/more reliable way to check if a window exists? Changing sending Alt+F4 to "close window event" might also worth a try, but I have no idea how to send this event to another program's window.

I'm a newbie, so please keep the answer simple.

Thanks in advance.


I would use Spy++ to watch the window handle of the Java app, and see if you can figure out what else is going on - I agree there has to be a way to tell that it is closed.

I assume watching the process list is out of the question...


I would hazard a guess that whilst the Java app is running and consequently, the JVM, the 'handle' to the window has not yet been garbaged collected and as such appears to the underlying pointer mechanism as still being valid.

If it was me writing that stuff (and if I was able to change the Java code) I'd probably add a means of querying the java app to see if its windows are showing. A sockets interface or something.


My guess is that either the window hasn't been completely disposed of by the Java code/VM, or it's handling Alt+F4 in some special way (i.e. maybe making itself invisible rather than closing).


Creation/deletion of windows is out of your control. If you want to reliably detect the presence of 'someone else' using the same resource a you want, have that other party communicate it explicitly.

The other party may signal it's presence by creating a file, opening a port, whatever, and can release/delete/close it on it's exit.


Try to make additional check using IsWindow API, on hWnd value returned by FindWindow.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号