Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for third party application window to load

I'm trying to create a C#.NET 2.0 script to automate a workflow in a third party application written in Java.

I’m using the user32.dll functions to be able to interact with the Java app windows.

My problem is that the Java app windows are slow to completely load and my action sent through IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, UInt32 wParam, IntPtr lParam) sometimes are lost. Example: during the load of the main window, if I try to open a menu (at this moment I actually have a handle on the main window), my message just is lost and the menu never opens.

I found some answers on Stack Overflow telling that we could use the Process.WaitForInputIdle() method for that purpose but I discovered that this only works if the app is mono threaded which is not the case of my Java app. ( C# SendKeys Wait for Program to Load before sending )

I'm looking for something working the same idea but that supports multithread or any other idea you could have?!: )

Following the Mari9' idea I finally used the System.Diagnostic.PerformanceCounter() to watch the third party app process activity. Then it commes to 0, it means the process has done what it had to do and is able to execute the message I send it.

PerformanceCounter PC = new PerformanceCounter();
PC.CategoryName = "Process";
PC.CounterName = "% Processor Time";
PC.InstanceName = "<process name>";
Console.WriteLine(PC.NextValue().ToString());
like image 699
Tuxicoman Avatar asked Jun 20 '12 13:06

Tuxicoman


1 Answers

I assume you have a way of detecting when the window you want to send input to is shown on screen, but sending input immediately does not work since the window is busy.

You could have a predefined/hardcoded wait interval (possibly customized for each Java window) - this is not very reliable, but may work well in a controlled environment (for instance if the app is used only on your computer).

If the appearance of the window changes reliably when loading is complete, you might capture a screenshot of the window and test to see if the right pixels are there, and repeat this until true before sending the message.

Another option is using Spy++ to see if any particular message is generated for a window when the loading is complete (eg. could be a repaint with certain parameters) and install a hook to detect those messages.

If the Java application uses native window controls (SWT), you could try to detect changes in the window texts/structure indicating that the load is complete.

If the window loading process is resource intensive you might also monitor the CPU usage of the java app, and wait for the end of the 'peak' before sending your message.

like image 159
Mari9 Avatar answered Nov 13 '22 17:11

Mari9