Jump to content

How to loop?


 Share

Recommended Posts

Hello im new to autoit and scripts.

I have made a very small script to prevent the opening of a program, heres the code

WinWaitActive("popups.exe")

WinClose("popups.exe")

MsgBox(0, "title", "Text")

Run("C:\Documents and Settings\HemPC\Skrivbord\autoitscript\popkill.exe")

I couldnt figure out how to do a loop of this part

WinWaitActive("popups.exe")

WinClose("popups.exe")

MsgBox(0, "title", "Text")

So i just made the program restart itself after killing the program and showing the msgbox.

Can some helpfull soul give me a brief tutorial of how to make a loop out of this, seeing as that would be a better option.

Also..Lets say i wanted to have the program wait for not only popups.exe to open, but also another program (not in any special order)..how should i write to make it kill not only popups.exe, but also the second program, and loop it after killing that also?

Im sorry if my explation of my need is weird, i dont know much about this program and either of programming..

Link to comment
Share on other sites

To do a loop forever, simply place it in a while loop. A while loop does something while a statement is true. If you have no statement and you just want it to loop forever, you simple say, "While 1". This means it will loop until 1 isn't 1 which will never happen.

This is the first way you requested:

While 1

WinWaitActive("popups.exe")
WinClose("popups.exe")
MsgBox(0, "title", "Text")


Wend

You also requested to have to do multiple windows. For that you would simply do this:

While 1

If WinExists("This is the title of the window") Then WinClose("This is the title of the window") 

If WinExists("This is the second window") Then WinClose("This is the second window")

sleep(20)

Wend

Notice how it loops and continues to do it looking for both windows each loop. The sleep function makes it so it pause for 20 milliseconds at the end of each loop. This prevents it from using up all your processing power. If you don't include the sleep function, your processor will do the loop as fast as it can which would use all of you processing power.

I hope that helped.

Edited by The Kandie Man

"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

To do a loop forever, simply place it in a while loop. A while loop does something while a statement is true. If you have no statement and you just want it to loop forever, you simple say, "While 1". This means it will loop until 1 isn't 1 which will never happen.

This is the first way you requested:

While 1

WinWaitActive("popups.exe")
WinClose("popups.exe")
MsgBox(0, "title", "Text")


Wend

You also requested to have to do multiple windows. For that you would simply do this:

While 1

If WinExists("This is the title of the window") Then WinClose("This is the title of the window") 

If WinExists("This is the second window") Then WinClose("This is the second window")

Wend

sleep(20)

Notice how it loops and continues to do it looking for both windows each loop. The sleep function makes it so it pause for 20 milliseconds at the end of each loop. This prevents it from using up all your processing power. If you don't include the sleep function, your processor will do the loop as fast as it can which would use all of you processing power.

I hope that helped.

Might want to move that sleep to the inside of the loop, otherwise won't help

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

Might want to move that sleep to the inside of the loop, otherwise won't help

Thanks, i was moving the brackets around for the code tag and i guess i pasted the sleep function outside the loop. :lmao:

"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

Thanks for the quick help !

Ok Kandie Man, the script you wrote worked very well and did close both the programs as they were opened, in any matter of order.

EDIT: never mind answering the quoted test below, i looked through some more helptopics and found out how to do what i wanted. Ill leave it as it is incase someone else should ever have the same issue.

Solution at the bottom.

"But then i realied i actually wanted to have a message displayed after closing the program.

so i tried this:

While 1

If WinExists("This is the title of the window") Then WinClose("This is the title of the window") And MsgBox( 0, "text", "text" )

sleep(20)

Wend

That didnt work, obviusly i dont know how to use the code, i just made a blind try.

Then i tried this:

While 1

WinWaitActive("popups.exe" )

WinClose( "popups.exe" )

MsgBox(0, "h", "h" )

WinWaitActive( "winprep.exe )

WinClose( "winprep.exe" )

MsgBox( 0, "h", "h" )

WEnd

But this would only work if i first tried starting popups.exe, then winprep.exe and not the other way around.

So..how do i include a MsgBox in my script? :lmao:"

This is what i wrote:

While 1

If WinExists("popus.exe") Then

WinClose("popups.exe")

MsgBox( 0, "title", "text" )

EndIf

If WinExists("winprep.exe") Then

WinClose("winprep.exe")

MsgBox( 0, "title", "text" )

EndIf

sleep(20)

Wend

Edited by whatever
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...