Sign in to follow this  
Followers 0
Freedom1

How to tell when program ends?

19 posts in this topic

Hello All,

I'm trying to write a script that runs a program (Windows GUI) and then close the program when the process finishes.

The Windows program has two buttons on it, "Start" and "Exit". I've tried "Run", "RunWait", etc. but haven't found a way to simulate the pressing of the "Exit" button when the "Start" process finishes.

Basically I want to run the program, then when the window becomes active I want to press the Start key, wait for the process to complete (1 - 3 minutes) and then when the Start process finishes, press the Exit key to exit from the program. There doesn't appear to be an indication that the process has finished except the buttons become available to click on once again.

Can someone point me to or supply an example?

Thanks,

Freedom1

P.S. I use the term "process" as the logic being executed in the program, not process as in PID.

Share this post


Link to post
Share on other sites



...There doesn't appear to be an indication that the process has finished except the buttons become available to click on once again...

ControlCommand/"IsEnabled", ""

Note: AutoIt only works with standard Microsoft controls - some applications write their own custom controls which may look like a standard MS control but may resist automation. Experiment!

-MSP-


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

Hello All,

I'm trying to write a script that runs a program (Windows GUI) and then close the program when the process finishes.

The Windows program has two buttons on it, "Start" and "Exit". I've tried "Run", "RunWait", etc. but haven't found a way to simulate the pressing of the "Exit" button when the "Start" process finishes.

Basically I want to run the program, then when the window becomes active I want to press the Start key, wait for the process to complete (1 - 3 minutes) and then when the Start process finishes, press the Exit key to exit from the program. There doesn't appear to be an indication that the process has finished except the buttons become available to click on once again.

Can someone point me to or supply an example?

Thanks,

Freedom1

P.S. I use the term "process" as the logic being executed in the program, not process as in PID.

Run("program.exe")
WinActivate("Window Title","Window Text")
WinWaitActive("Window Title","Window Text")
ControlClick("Window Title", "", "Start"); click the start button
; Wait for process to complete here... i'm not sure what your program is doing so it's hard to use an example here
; Is it running any external programs or background processes?
WinActivate("Window Title","Window Text")
WinWaitActive("Window Title","Window Text")
ControlClick("Window Title","","Exit"); click the exit button

I suppose if the part of the script you are having problems with is finding out when the "process" is finished running, you could just use a While loop to continue trying to click Exit until it succeeds. :rolleyes:

While WinExists("Window Title","Window Text")
    WinActivate("Window Title","Window Text")
    WinWaitActive("Window Title","Window Text")
    ControlClick("Window Title","","Exit")
WEnd

Certifications: A+, Network+, Security+, Linux+, LPIC-1, MCSA | Languages: AutoIt, C, SQL, .NETBooks: AutoIt v3: Your Quick Guide - $7.99 - O'Reilly Media - September 2007-------->[u]AutoIt v3 Development - newbie to g33k[/u] - Coming Soon - Fate Publishing - Spring 2013UDF Libraries: SkypeCOM UDF Library | ADUC Computers OU Cleanup | Find PixelChecksumExamples: Skype COM Examples - Skype4COMLib Examples converted from VBS to AutoIt

Share this post


Link to post
Share on other sites

It sounds like he's running a program that seems to just do some background changes in the same PID. He may have to use ControlCommand("title","text",ControlID,"IsEnabled") in a loop to wait for the Exit button to become "clickable." The example loop I created would have the same outcome, it would just try to click Exit until it succeeded (since it'll be grayed out until completion).


Certifications: A+, Network+, Security+, Linux+, LPIC-1, MCSA | Languages: AutoIt, C, SQL, .NETBooks: AutoIt v3: Your Quick Guide - $7.99 - O'Reilly Media - September 2007-------->[u]AutoIt v3 Development - newbie to g33k[/u] - Coming Soon - Fate Publishing - Spring 2013UDF Libraries: SkypeCOM UDF Library | ADUC Computers OU Cleanup | Find PixelChecksumExamples: Skype COM Examples - Skype4COMLib Examples converted from VBS to AutoIt

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

ControlCommand/"IsEnabled", ""

as in

do

sleep(100)

until ControlCommand("title","text",ControlID,"IsEnabled","")

ControlClick....

-MSP-

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

It sounds like he's running a program that seems to just do some background changes in the same PID. He may have to use ControlCommand("title","text",ControlID,"IsEnabled") in a loop to wait for the Exit button to become "clickable." The example loop I created would have the same outcome, it would just try to click Exit until it succeeded (since it'll be grayed out until completion).

Thanks alot to all who responded.

Airwolf123, you're correct - background changes in the same PID. Although I had to make some changes to the examples given, it works as I need it to work. It does seem though that my code could be tightened up somewhat. Here's my testing code:

CODE
Run("C:\DDR\Calc200e.exe")

WinActivate("Form1","")

WinWaitActive("Form1","")

ControlClick("Form1", "", "Process"); click the start button

;************* The process is now running

WinActivate("Form1","")

WinWaitActive("Form1","")

While ControlCommand ( "Form1", "", "Exit", "IsEnabled", "" ) = 0

WEnd

ControlClick("Form1","","Exit")

Exit

I don't know why I have to do the WinActivate & WinWaitActive after the process is running. Is the While command to "tight"?

This AutoIt sure is loaded with features!

Thanks again,

Freedom1

Share this post


Link to post
Share on other sites

as in

do

sleep(100)

until ControlCommand("title","text",ControlID,"IsEnabled","")

ControlClick....

-MSP-

Your code looks better and more efficient than mine. I'm changing to yours.

Thanks,

Freedom1

Share this post


Link to post
Share on other sites

sorry - I posted about the same time that you did - with about the same loop.

Add a sleep to your While/Wend or your compete for CPU time during the installation.

You can do without the WinActivate & WinWaitActive - like you said.


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

sorry - I posted about the same time that you did - with about the same loop.

Add a sleep to your While/Wend or your compete for CPU time during the installation.

You can do without the WinActivate & WinWaitActive - like you said.

I just like to throw those in just in case the window loses focus. :rolleyes: Using a loop with ControlCommand is a much "neater" way to solve Freedom1's issue than my example anyway.


Certifications: A+, Network+, Security+, Linux+, LPIC-1, MCSA | Languages: AutoIt, C, SQL, .NETBooks: AutoIt v3: Your Quick Guide - $7.99 - O'Reilly Media - September 2007-------->[u]AutoIt v3 Development - newbie to g33k[/u] - Coming Soon - Fate Publishing - Spring 2013UDF Libraries: SkypeCOM UDF Library | ADUC Computers OU Cleanup | Find PixelChecksumExamples: Skype COM Examples - Skype4COMLib Examples converted from VBS to AutoIt

Share this post


Link to post
Share on other sites

I just like to throw those in just in case the window loses focus. :rolleyes: Using a loop with ControlCommand is a much "neater" way to solve Freedom1's issue than my example anyway.

Here's the portion of my code that now works properly. (Without it, if the window was changed, the window would never be in focus and the statements following "WinWaitNotActive" would be executed.

do

sleep(100)

WinActivate("Form1","")

WinWaitActive("Form1","")

until ControlCommand ( "Form1", "", "Exit", "IsEnabled", "" )

ControlClick("Form1","","Exit")

WinWaitNotActive("Form1")

Now I'm wondering if I should use "WinWaitClose" rather than "WinWaitNotActive" .

Any suggestions?

Freedom1

Share this post


Link to post
Share on other sites

It depends on what comes next in your script, if you are going to just exit, then you do not even need WinWaitNotActive("Form1"). If there is another window to act on, then you could just use WinWait......MSP


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

It depends on what comes next in your script, if you are going to just exit, then you do not even need WinWaitNotActive("Form1"). If there is another window to act on, then you could just use WinWait......MSP

Well I thought I was home free, but I was WRONG!

I had been testing the following code making changes to make it bullet-proof so I could complete the rest of the tasks (basically running GUI programs and turning it into a batch process). I came across the problem if the called program (Calc200e.exe) is minimized, then the code I have in the following codebox does not work when attempting to "Click on the Exit button". Any suggestions to resolve?

Thanks,

Freedom1

CODE
$PID = Run("C:\DDR\Calc200e.exe")

WinActivate("Form1", "")

WinWaitActive("Form1", "")

ControlClick("Form1", "", "Process"); click the start button

;************* The process is now running

Do

Sleep(100)

WinActivate("Form1", "")

WinWaitActive("Form1", "")

Until ControlCommand("Form1", "", "Exit", "IsEnabled", "")

ControlFocus("Form1", "", "Exit")

ControlClick("Form1", "", "Exit")

WinWaitClose("Form1", "")

MsgBox(0, "This is a test.", "Check to see if monte is still active.")

;

;other code goes here to run another program with similar code to above

;

Share this post


Link to post
Share on other sites

...if the called program (Calc200e.exe) is minimized, then the code I have in the following codebox does not work when attempting to "Click on the Exit button"...

Are you saying that the program can some how be minimized after the WinActivate("Form1", "") line and before the ControlClick line?

The Do/Until loop should keep the window active (not minimized).


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

Are you saying that the program can some how be minimized after the WinActivate("Form1", "") line and before the ControlClick line?

The Do/Until loop should keep the window active (not minimized).

No, I'm not saying that - I don't think. Its just that this piece of code runs differently many of the times that I run it.

The program Calc200e does some minimal db access and a few calculations. I ran the code 5 times. 3 of those 5 times it ran as expected. The other two times it APPEARED that the Exit key was not clicked and did not close Calc200e until I manually clicked on the exit button.

I'm just learning about Autoit and I'm unsure of the nuances it provides. I'm going to try using PID's instead of window text to see if that cleans it up a bit.

Thanks for sticking with me on this as I'm learning,

Freedom1

Share this post


Link to post
Share on other sites

...Thanks for sticking with me on this as I'm learning,

no problem

try this:

;************* The process is now running
Do
    Sleep(100)
    WinActivate("Form1", "")
    WinWaitActive("Form1", "")
Until ControlCommand("Form1", "", "Exit", "IsEnabled", "")

While WinWaitClose("Form1", "", 1) = 0
    WinActivate("Form1", "")
    WinWaitActive("Form1", "",1)
    ControlFocus("Form1", "", "Exit")
    ControlClick("Form1", "", "Exit")
WEnd

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

no problem

try this:

;************* The process is now running
Do
    Sleep(100)
    WinActivate("Form1", "")
    WinWaitActive("Form1", "")
Until ControlCommand("Form1", "", "Exit", "IsEnabled", "")

While WinWaitClose("Form1", "", 1) = 0
    WinActivate("Form1", "")
    WinWaitActive("Form1", "",1)
    ControlFocus("Form1", "", "Exit")
    ControlClick("Form1", "", "Exit")
WEnd

Appreciate the code. I tried it and lo and behold it WORKED!! --- Wait --- I ran it about five times and then it began to exhibit the same problem I had been having - would not close the called program. Used trace lines and it was looping in the "While" statement.

I spent more time looking thru the forum and found one other issue similar to mine that has not been resolved yet. (Unfortunately I didn't record the topic so I could refer to it again.)

I'm going to create a new topic "ControlClick not working as expected!! Help." to see if I can get some ideas on how to make this process run reliably.

Thanks,

Freedom1

Share this post


Link to post
Share on other sites

You might read the help file under "Controls":

One or more properties are used in the controlID parameter of a control command in the format: [PROPERTY1:Value1; PROPERTY2:Value2]

If you were to copy/paste/post the info from the summary tab of the AutoIt Window Info tool for that control of interest, maybe someone could help construct a better way to deal with it.

Also note that some apps just have flaky controls.


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

Why not simply use WinClose instead of that WinWaitClose loop with a ControlClick?


"be smart, drink your wine"

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0