Jump to content

Easy one...Loops and While expression question


Recommended Posts

Hi,

I have written a script for updating a program. I need part of this script to check a process and if open pause and start again at the top of the script (which starts with and If statement) in a specified time or wait until that process closes....

Have looked at While and looping but cannot make sense of it...If someone could guide me to the best command to use I wil tough it lout from there....I also saw and article from LxP which he discussed some commands causing a Stack overflow...nasty and need to avoid that...

Thanks

Mike

Edited by hispeed_mike
Link to comment
Share on other sites

maybe

$MyProcess = Run("notepad.exe")
$t = ""
While ProcessExists($MyProcess)
    Sleep(20)
    $t = $t +1
    If $t > 200 Then ProcessClose($MyProcess)
WEnd

MsgBox(0,"test ok!","process closed and passed the loop")

8)

Could I bother you to give me a quick run down on this as I do not quite understand the use of variables such as $t....

The process I am waiting on being closed is IE. It will be closed by another application so I want the script to start the If statement which looks to see if Ie is open...if it is I want the script to pause then recheck after say 20 minutes and then start at top of script again. If Ie is open then just wait and retry again in another 20 minutes...

Hope this makes sense...

Mike

Link to comment
Share on other sites

Ok, like this

while 1

If ProcessExists("iexplore.exe") Then
    MsgBox(0,"Found","Internet Explorer is running.")
Else
    MsgBox(0,"Not Found","Internet Explorer is not running.")
EndIf
sleep(1200000);this is 20 minutes since it is on milliseconds.  1000 milliseconds = 1 second. 1000*60*20 = 1200000
WEnd

Is that what you wanted?

"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Link to comment
Share on other sites

Ok, like this

while 1

If ProcessExists("iexplore.exe") Then
    MsgBox(0,"Found","Internet Explorer is running.")

Else
    MsgBox(0,"Not Found","Internet Explorer is not running.")

EndIf
sleep(1200000);this is 20 minutes since it is on milliseconds.  1000 milliseconds = 1 second. 1000*60*20 = 1200000
WEnd

Is that what you wanted?

while 1

If ProcessExists("iexplore.exe") Then

MsgBox(0,"Found","Internet Explorer is running.")

Need it to sleep if iexplore is found - then after sleep check iexplore again and continue this until iexplore has closed and run rest of script

Else

MsgBox(0,"Not Found","Internet Explorer is not running.")

would continue script with other commands here

EndIf

sleep(1200000);this is 20 minutes since it is on milliseconds. 1000 milliseconds = 1 second. 1000*60*20 = 1200000

WEnd

I just don't know how to script the looping of the increment that checks is IE is open...As far as i read the above script it would check to see if IE is open or closed and then put a message box up...then sleep for 20 minutes but I don't see how it then starts the script again??????????

Edited by hispeed_mike
Link to comment
Share on other sites

hispeed_mike,

You said, Have looked at While and looping but cannot make sense of it...

So lets deal with that first. The help file states this for While/WEnd:

While <exp[b][/b]ression>
    statements
    ...
WEnd

If the expression is true

the following statements

up to the WEnd statement

are executed.

This loop continues until

the expression is false.

In this code:
While 1 = 1
    MsgBox(0,"AutoIt","Loop Test")
WEnd
MsgBox(0,"AutoIt","Code for line 4")
1 = 1 is the expression that is true

and the Loop Test MsgBox will

show up over and over until

you manually kill the script.

You will never see the last MsgBox from the code on line 4

- because the loop will never exit...

- because the expression is always true.

If the code looked like this:

While 1 = 2
    MsgBox(0,"AutoIt","Loop Test")
WEnd
MsgBox(0,"AutoIt","Code for line 4")
You should never see the Loop Test MsgBox since 1 = 2 is false.

Line 2 will be skipped because 1 = 2 is false.

You will only see the last MsgBox from the code on line 4.

Back to the help file for While/WEnd:

To create an infinite loop, you can use a non-zero number as the expression.

Hopefully you understand that 1 = 1 is true and 1 = 2 is false.

Add to that understanding that 1 equals true.

The number 1 all by itself is true.

The number 2 all by itself is true.

The number 1123 all by itself is true.

...and so on...

(Zero, all by itself is false.)

In this code:

While 1
    MsgBox(0,"AutoIt","Loop Test")
WEnd
MsgBox(0,"AutoIt","Code for line 4")
What do you think will happen?

(It acts the same as having While 1 = 1)

In this code:

While 0
    MsgBox(0,"AutoIt","Loop Test")
WEnd
MsgBox(0,"AutoIt","Code for line 4")
What do you think will happen?

(It acts the same as having While 1 = 2)

If Internet Explorer (IE) is running,

the AutoIt function named ProcessExists

should return a non-zero number for the process.

Run this one line of code:

MsgBox(0,"AutoIt",ProcessExists("iexplore.exe"))
While IE is running, you should get some number.

While IE is not running, you should get 0 (or false).

Combine While/WEnd with ProcessExists to get:

While ProcessExists("iexplore.exe")
    MsgBox(0,"AutoIt","Loop Test")
WEnd
MsgBox(0,"AutoIt","Code for line 4")

If you replaced the "Loop Test" MsgBox with a short sleep statement:

While ProcessExists("iexplore.exe")
    Sleep(1000)
WEnd
MsgBox(0,"AutoIt","Code for line 4")
The script will just sit there and loop until IE is closed.

In your original post:

You said, ...I need part of this script to check a process and if open pause and start again at the top of the script. You could put the While/WEnd loop shown above at the top of your script and hopefully by now you understand what that loop will do... but once IE has exited and is no longer in the process list, your script will continue past that While/WEnd loop. If the user starts IE again while other lines of your script are executing... the While/WEnd loop described above will not pause your script again and it will not "start again at the top of the script". [We are not going to talk about the Adlib function for now.]

Hope this helps...

Edited by herewasplato

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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...