Check if Windows Application is running (not process)

廉价感情. 提交于 2019-12-12 03:37:06

问题


I am hoping to check at the beginning of an automated test if an application is open. I can check if the process is running by doing the following

foreach (Process proc in Process.GetProcesses()) 
{
    if (proc.ProcessName.Contains(name))
    {
        return true;
    }
}

However, the process I want to find starts up about a minute before the application actually opens and is ready to be used by the test methods (its a very slow starting application). The above code sample looks at all windows processes running, but I am wondering, is there a way to do a similar method but to look at windows applications running?


回答1:


There is a method already in class Process that you can use to check if an app with a UI has fully started:

Process.WaitForInputIdle(int milliseconds)

This will wait up to milliseconds ms for the message loop to become idle (and returns a bool to indicate success status). Depending on the application you're waiting for, you might want to allow 30 seconds or longer.

This might work for you, but be aware that in my experience for some applications it is not totally reliable!

The Windows API documentation has more details about the Windows API function that WaitForInputIdle() calls behind the scenes.




回答2:


When a process is started, you can say application has started. What you want is to wait until application startup progress has completed or not.

This means, when process is started, application startup begins. When application startup is completed, is becomes ready for user input. So I think you should have a look at following question and its answers.

Programmatically, how does this application detect that a program is ready for input




回答3:


Apllication is proces. If you can modify app, at app start you can create file and at end delete it. So you can chceck file existance. If file exist app starting/started.

If you need info when main form is created use:

WINFORMS Form.Shown event.

WPF Loaded Event




回答4:


uITestControl.Exists did the trick for me.

This method will return a boolean value corresponding to the existence of the application window being open. This allows an if statement to be created that can open the application if not already open, or do nothing if its already open.



来源:https://stackoverflow.com/questions/38569088/check-if-windows-application-is-running-not-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!