Jump to content

While if WEnd Else


 Share

Go to solution Solved by david1337,

Recommended Posts

Hi guys

What am I doing wrong in my example here?

I want the script to loop from While 1 to WEnd until there is no WinExists("Test.txt")

When there is no WinExists("Test.txt") I want it to go to Else

MsgBox (1, "info" ,"Are you sure you want to open Notepad?")
 
While 1 

ProcessClose("notepad.exe")

sleep(3000)

ShellExecute("notepad.exe")



Sleep(5000)

AutoItSetOption("WinTitleMatchMode", 2)
if WinExists("Test.txt") Then

WEnd


Else
   
   
MsgBox(0, "INFO", "Test.txt is no longer excisting")

EndIf
Exit
Edited by david1337
Link to comment
Share on other sites

https://www.autoitscript.com/autoit3/docs/functions/WinExists.htm

The documentation specifically gives an example of how to use this function with notepad. You can try WinExists("notepad.exe"). If you only have one instance of notepad open at a time you can also try processexists("notepad.exe"). Even if you have more than one instance of notepad that you want to control then use WinGetProcess.

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Link to comment
Share on other sites

Hey mate

Thanks for answering.

Okay I think the notepad example was bad.

I am trying to create a shutdown script, that executes my sync program first.

Sometimes when the program is opened, it goes "not responding"

So I want the script to redo the whole thing if it detects the not "responding" in the name.

I want it to redo the script until it executes Allway sync without going into "not responding"

 

So if it doesn't detect "Not responding" in the name 5 seconds after it's executed, then it should continue to "Else"

See script below:

MsgBox (1, "Shutdown" ,"Are you sure you want to shut down?")
 
While 1

ProcessClose("syncappw.exe") ;close the program if already running

sleep(3000)

ShellExecute("C:\Program Files (x86)\Allway Sync14\Bin\syncappw.exe", "-s -e") ;open it again with parameters that starts sync and ends program when sync is done

sleep(5000)


AutoItSetOption("WinTitleMatchMode", 2)
if WinExists("Not Responding") Then

WEnd


Else
   
ProcessWaitClose("syncappw.exe")
   
MsgBox(0, "INFO", "Computer is now shutting down");Here the Shutdown command will come in

EndIf
Exit
Edited by david1337
Link to comment
Share on other sites

Your block constructs intersect, which is not correct.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

maybe something like this?

 

 

If MsgBox(1, "Shutdown", "Are you sure you want to shut down?") = 1 Then
    AutoItSetOption("WinTitleMatchMode", 2)
    Do
        If ProcessExists("syncappw.exe") Then ProcessClose("syncappw.exe") ;close the program if already running
        ProcessWaitClose("syncappw.exe")
        $pid = ShellExecute("C:\Program Files (x86)\Allway Sync14\Bin\syncappw.exe", "-s -e") ;open it again with parameters that starts sync and ends program when sync is done
    Until Not WinWait("Not Responding", '', 5)

    MsgBox(0, "INFO", "Computer is now shutting down");Here the Shutdown command will come in
EndIf
Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
Share on other sites

bogQ

Brilliant! Thank you!

I think that might did it!

I can tell if it worked the next time the program goes "Not responding" :)

This is how the script looks like now:

If MsgBox(1, "Shutdown", "Are you sure you want to shut down?") = 1 Then     

AutoItSetOption("WinTitleMatchMode", 2)

Do         

If ProcessExists("syncappw.exe") Then ProcessClose("syncappw.exe") ;Closes Allway Sync, so it's ready to execute again with the correct parameters

ProcessWaitClose("syncappw.exe")         

$pid = ShellExecute("C:\Program Files (x86)\Allway Sync14\Bin\syncappw.exe", "-s -e") ;Starts Allway sync with the parameters -s -e -->  [-s = start all sync jobs]   [-e = exit Allway sync when sync jobs are done]  

Sleep(4000)

ShellExecute("notepad") ;notepad is used to take fokus from Allway sync, so that ("!{TAB}") will give Allway sync focus again in line 21

ProcessWait("notepad.exe")

Sleep(5000)

send("!{TAB}") ;Gives focus on Allway sync again - This forces the "Not responding" to apply in the window title, if the program is frozen anyway - (winactivate does not give the same effect, therefore this clumsy soloution)

sleep(2000)

ProcessClose("notepad.exe") ;notepad not needed anymore

sleep(3000)

Until Not WinWait("Not Responding", '', 5)     

ProcessWaitClose("syncapp.exe")

MsgBox(0, "INFO", "Computer is now shutting down");Here the Shutdown command will come in 

EndIf
Link to comment
Share on other sites

It didn't work :(

It displays the "Computer is now shutting down" message while Allway sync is still running the sync jobs.

Maybe it's because it detects the ProcessClose("syncappw.exe")  earlier in the script? Then the ProcessWaitClose("syncapp.exe") reacts and the message is shown.

ProcessWaitClose("syncapp.exe") must not react before Allway sync has closed by itself! (the -e parameter)

Edited by david1337
Link to comment
Share on other sites

i don`t know what is happening on your machine but i did load pid to variable so try to edit last 3 lines to this

ProcessWaitClose($pid)

MsgBox(0, "INFO", "Computer is now shutting down");Here the Shutdown command will come in

EndIf

basically the process you run process need be checked do it still exsist

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
Share on other sites

Try maybe using some variable in While statement and change it to 0 when u want to end the loop?

#include <MsgBoxConstants.au3>

Local $q_run = 1
Local $_button_id

While $q_run
    sleep(3000)
    If MsgBox(1,"","Your code here...") = $IDOK Then    ;if you press OK button...
        $q_run = 0                                      ;$run will be set to 0 and WHILE loop will end...
    EndIf
WEnd
MsgBox(0,"","Exiting - outside WHILE loop!")
Link to comment
Share on other sites

  • Solution

Thanks for all your help guys.

I ended out solving it with a simple call func.

So the final working script looks like this:

AutoItSetOption("WinTitleMatchMode", 2)

;___________________________________________________________;

$MsgBox = MsgBox(4, "Shutdown", "Are you sure you want to shut down?")
If $MsgBox = 6 Then  
Call("Startover")
EndIf

;___________________________________________________________;


Func Startover()

ProcessClose("syncappw.exe") ;Closes Allway Sync, so it's ready to execute again with the correct parameters

ProcessWaitClose("syncappw.exe")

ShellExecute("C:\Program Files (x86)\Allway Sync14\Bin\syncappw.exe", "-s -e") ;Starts Allway sync with the parameters -s -e -->   [-s = start all sync jobs]   [-e = exit Allway sync when sync jobs are done]

Sleep(4000)

ShellExecute("notepad") ;notepad is used to take fokus from Allway sync, so that ("!{TAB}") will give Allway sync focus again

ProcessWait("notepad.exe")

Sleep(5000)

send("!{TAB}") ;Gives focus on Allway sync again - This forces the "Not responding" to apply in the window title, if the program is frozen anyway - (winactivate does not give the same effect, therefore this clumsy soloution)

sleep(1000)

ProcessClose("notepad.exe") ;notepad not needed anymore

sleep(6000)


if WinExists("Not Responding") Then

Call("Startover")


Else
   
ProcessWaitClose("syncappw.exe")

shutdown(5) ;[(1) = Shutdown]  [(4) = Force]

EndIf
EndFunc
Edited by david1337
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...