Sign in to follow this  
Followers 0
johnmcloud

[SOLVED] While...WEnd with multiple if expression

13 posts in this topic

#1 ·  Posted (edited)

Maybe is a stupid question, but i have a doubt.

i'd like to know a correct way to use a multiple if expression in a While...WEnd loop.

Example:

When i need a conditional If...EndIf in a loop i'll use everytime a Do...Until:

Do
Sleep(100)
Until ProcessExists("notepad.exe")

MsgBox(0,0,0)

Work fine, but if i use a While...WEnd?

While 1
If ProcessExists("notepad.exe") Then
     Test()
     ExitLoop
Else
     Sleep(100)
EndIf
If ProcessExists("wordpad.exe") Then
     Test()
     ExitLoop
Else
     Sleep(100)
EndIf
WEnd

Func Test()
MsgBox(0, 0, 0)
EndFunc ;==>Test

If i open notepad, i have only one msgbox but the script exit from the loop ( exitloop ), so if i open wordpad nothing happened

In the example my goal is:

1) Open notepad --> One Msgbox

2) Open wordpad --> One Msgbox

3) Don't exit from the loop

4) Use only one While...WEnd

So or I see infinite msgbox because i can't stop the loop, or i exit from the loop and i can't have the second If...Then. How to resolve?

Thanks

Edited by johnmcloud

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

Hi JohnOne,

If i use your solution, i can't put anything else in the While..WEnd, so is the same like a Do...Until. I'd like to use the While..WEnd for use only one loop for different function, but with only one MsgBox/ConsoleWrite/Run whatever

Edited by johnmcloud

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Well your code above is fine.

While 1
    If ProcessExists("notepad.exe") Then
        MsgBox(0, 0, 0)
        ExitLoop
    Else
        Sleep(100)
    EndIf
WEnd

1 msgbox, if there are more then I suspect there is more going on in your loop than

you posted.

And why can't you exit the loop here?

While 1
    If ProcessExists("notepad.exe") Then
        Test()
        ExitLoop
    Else
        Sleep(100)
    EndIf
WEnd

Func Test()
    MsgBox(0, 0, 0)
EndFunc   ;==>Test
Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

The first solution give me 2 MsgBox, don't know why, the second only one.

But the problem is another for me, check this:

While 1
If ProcessExists("notepad.exe") Then
     Test()
     ExitLoop
Else
     Sleep(100)
EndIf
If ProcessExists("wordpad.exe") Then
Test()
     ExitLoop
Else
     Sleep(100)
EndIf
WEnd

Func Test()
MsgBox(0, 0, 0)
EndFunc ;==>Test

If i open notepad, i have only one msgbox but the script exit from the loop ( exitloop ), so if i open wordpad nothing happened

In the example my goal is:

1) Open notepad --> One Msgbox

2) Open wordpad --> One Msgbox

3) Don't exit from the loop ( i know i have put exitloop in the first post, my mistake )

4) Use only one While...WEnd

EDIT: Update the first post, hope more clear now

Edited by johnmcloud

Share this post


Link to post
Share on other sites

Probably a better way f doing this but thought id give it a shot still learning myself see.

I used Chrome, which brought up two message boxes i was too lazy to find wordpad on windows 8

I used ProcessClose to close the application, you could use Sleep(5000) for 5 seconds to close it yourself though.

While 1
If ProcessExists ("chrome.exe") Then
test()
ProcessClose("chrome.exe")
ElseIf ProcessExists ("notepad.exe") Then
test()
ProcessClose("notepad.exe")
Else
Sleep(100)
EndIf
WEnd

Func Test()
    MsgBox(0, 0, 0)
EndFunc   ;==>Test

Share this post


Link to post
Share on other sites

Update:

ProcessWaitClose - User needs to close the window rather than it being automatic

Test() updated for personal message as seen below.

While 1
If ProcessExists ("chrome.exe") Then
test("Chrome is open")
ProcessWaitClose("chrome.exe")
ElseIf ProcessExists ("notepad.exe") Then
test("Notepad is open")
ProcessWaitClose("notepad.exe")
Else
Sleep(100)
EndIf
WEnd

Func Test($message)
MsgBox(0, 0, $message)
EndFunc ;==>Test

Share this post


Link to post
Share on other sites

Local $NoteFlag = 0
Local $WordFlag = 0

While 1
    If ProcessExists("notepad.exe") And Not $NoteFlag Then
        $NoteFlag = 1
        Test()
        ;ExitLoop
    Else
        Sleep(100)
    EndIf
    If ProcessExists("wordpad.exe") And Not $WordFlag Then
        $WordFlag = 1
        Test()
        ;ExitLoop
    Else
        Sleep(100)
    EndIf
WEnd

Func Test()
    MsgBox(0, 0, 0)
EndFunc   ;==>Test

If it's something else, then You will have to explain what you are doing a little more detailed.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

So, i have tested the JohnOne's script and make a little change:

Local $NoteFlag = 0
Local $WordFlag = 0

While 1
    If ProcessExists("notepad.exe") And Not $NoteFlag Then
        $NoteFlag = 1
        $WordFlag = 0
  Test()
    Else
  Sleep(100)
    EndIf
    If ProcessExists("wordpad.exe") And Not $WordFlag Then
        $WordFlag = 1
        $NoteFlag = 0
  Test()
    Else
  Sleep(100)
    EndIf
WEnd

Func Test()
    MsgBox(0, 0, 0)
EndFunc   ;==>Test

Now work in this way:

1) Open notepad --> Msgbox

2) Close notepad --> Open wordpad --> Msgbox

3) Close wordpad --> Open notepad --> Msgbox

4) etc.

There is only one problem, this:

1) Open notepad --> Msgbox

2) Close notepad, Open notepad --> Nothing

How resolve this? Any idea?

Thanks for the help

Edited by johnmcloud

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

I think i have resolved, this the final example script:

Local $NoteFlag = True, $WordFlag = True

While 1
 If ProcessExists("notepad.exe") And $NoteFlag Then
  $NoteFlag = False
  $WordFlag = True
  _StatusMsgBox("You have open Notepad")
 ElseIf Not ProcessExists("notepad.exe") And Not $NoteFlag Then
  $NoteFlag = True
  _StatusMsgBox("You have close Notepad")
 Else
  Sleep(100)
 EndIf
 If ProcessExists("wordpad.exe") And $WordFlag Then
  $WordFlag = False
  $NoteFlag = True
  _StatusMsgBox("You have open WordPad")
 ElseIf Not ProcessExists("wordpad.exe") And Not $WordFlag Then
  $WordFlag = True
  _StatusMsgBox("You have close WordPad")
 Else
  Sleep(100)
 EndIf
WEnd

Func _StatusMsgBox($handle)
 MsgBox(0, 0, $handle)
EndFunc   ;==>Test

You can open notepad and wordpad in any way, you have everytime the Func.

Thanks to JohnOne and also to Gotemp, it's a good thing for a new user try to be useful ;)

Edited by johnmcloud

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Hi John from your explaining you want the following...

Start Notepad = MsgBox > You close notepad

Start Word = MsgbBox > You close word

Start Notepad = MsgBox > You close notepad

Start Word = MsgbBox > You close word

Can you tell me what my code did your end because my end it did as you explained.

If its that you also want a closing message then (yes i used mspaint i got fed up of closing my browser =P):

While 1
If ProcessExists ("mspaint.exe") Then
test("mspaint is open")
ProcessWaitClose("mspaint.exe")
Test("mspaint was closed") ; <=== close message
ElseIf ProcessExists ("notepad.exe") Then
test("Notepad is open")
ProcessWaitClose("notepad.exe")
Test("notepad was closed") ; <=== close message
Else
Sleep(100)
EndIf
WEnd

I'll also add your doing alot unnecessary typing :/

Edited by Gotemp

Share this post


Link to post
Share on other sites

Glad you got it worked out johnmcloud.

When you progress, and start hearing stories about how evil global variables are, look at

the static keyword and employ that, so you can change a flag within a function and have it

persist throughout your project.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Gotemp,

I think you have misunderstood what is the goal of this thread.

I don't wanto to close-open anything, the ProcessExist was only an example, your script work only because you have add the ProcessClose after the ProcessExist.

But if you want only to know if a process exist? And then start example Notepad without close the first process? You need also the all unecessary code with the flag, or you'll start an infinite number of notepad :D

Anyway, i'll repeat, my scripit was only an example for know the best way to add in a single loop multiple if statement with only one msgbox/run/consolwrite or anything else.

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