0

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?

Pseudo Sudo
  • 1,402
  • 3
  • 16
  • 34
  • 1
    What is the difference in your mind between "windows processes" and "windows applications"? – adv12 Jul 25 '16 at 13:32
  • Looking at the task manager, there is a tab for both Applications and Processes. The process that corresponds to my application starts running and shows up in the processes menu right when the icon is clicked, however the application doesn't actually show up in the Applications menu until some time later when the window actually opens. I want to ensure that this window is open.. not just that the process is running. I'm open to any method to check this. – Pseudo Sudo Jul 25 '16 at 13:35
  • @Nate winforms or WCF? – BWA Jul 25 '16 at 13:41
  • @BWA I believe it is a WPF – Pseudo Sudo Jul 25 '16 at 13:45
  • If you can modify slow-starting application then you can add synchronization to check if it's started (or alternatively write a start application for it, but in this case you have to ensure what start application is always used to start it). Named `Mutex` is a good candidate. – Sinatr Jul 25 '16 at 14:19

4 Answers4

3

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.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • Is there any way for me to check if the process is ready for user input? Rather than waiting for it to be ready? What if it is already ready? – Pseudo Sudo Jul 25 '16 at 14:03
  • @Nate `WaitForInputIdle()` DOES check that it's ready for user input. And if it's already ready, `WaitForInputIdle()` returns immediately. Did you read the documentation in the links I posted in the answer? – Matthew Watson Jul 25 '16 at 14:06
  • What I was looking for is 'uITestControl.Exists'. With this I can do `if(uITestControl.Exists == true){ //Application is ready for use } else { //Application is not ready }` You just need to ensure that the `uITestControl` is a piece of the application that will only be active when the application is actually running. Thanks for the help! – Pseudo Sudo Jul 25 '16 at 14:10
  • @Nate That does not match what you asked in your question... You can use the process name to find the matching name, and then use `Process.WaitForInputIdle()` to wait for it to be ready. It's not clear to me why that wouldn't work for you. – Matthew Watson Jul 25 '16 at 14:19
  • it wouldn't work for me because there is a chance that the application has not been opened yet. In this situation the process would not exist. Using `WaitForInputIdle` doesn't give me a way to check the existence of that, where the `Exists` property does. – Pseudo Sudo Jul 25 '16 at 14:39
  • @Nate Are you saying that the following in your question is incorrect? `the process I want to find starts up about a minute before the application actually opens`? I interpreted that to mean that the application takes a long time to show its main window after it has been launched (but its process has already shown up in the `Process.GetProcesses()` list). If that's the case, then you can use `WaitForInputIdle()`. – Matthew Watson Jul 25 '16 at 15:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118239/discussion-between-nate-and-matthew-watson). – Pseudo Sudo Jul 25 '16 at 15:24
1

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

Community
  • 1
  • 1
Kamalesh Wankhede
  • 1,475
  • 16
  • 37
  • This is closer to what I am looking for, but how can I use this to check if the application is already ready for user input. Is there any boolean attribute that I can check so I can say `if(InputIdle == true) { //Do something } else //Open application` – Pseudo Sudo Jul 25 '16 at 13:59
  • According to [this](http://stackoverflow.com/q/1404658/6170142) it is generally impossible to detect since you are asking if any third party application is ready for user input. Have a look at [this](http://stackoverflow.com/q/1404658/6170142) question. – Kamalesh Wankhede Jul 25 '16 at 14:05
0

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

BWA
  • 5,672
  • 7
  • 34
  • 45
  • This would work for me, however, I need to ensure that the application has fully loaded and is ready to be used before doing this.. which is the problem I'm trying to solve.. hope that makes sense – Pseudo Sudo Jul 25 '16 at 13:37
0

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.

Pseudo Sudo
  • 1,402
  • 3
  • 16
  • 34
  • 1
    While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations! – Blue Jul 26 '16 at 04:21