dphuc23 Posted July 23, 2021 Share Posted July 23, 2021 Hi all Is had a problem when using For loop. I want to stop the For loop when Internet connection is lost but it still execute. Here is my code. Is there anything wrong? #include <Constants.au3> #include <WinAPIDiag.au3> For $i = 1000 To 1 Step -1 If _WinAPI_IsInternetConnected() Then ConsoleWrite($i & @CRLF) Sleep(1000) ; The variable $i will decrease by 1 unit every second Else ConsoleWrite("Connection no exists" & @CRLF) Sleep(1000) EndIf Next Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted July 23, 2021 Share Posted July 23, 2021 ExitLoop Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
dphuc23 Posted July 23, 2021 Author Share Posted July 23, 2021 3 minutes ago, FrancescoDiMuro said: ExitLoop It will exit the For loop. I want the loop to stop and continue running after the network connection is back Link to comment Share on other sites More sharing options...
Developers Solution Jos Posted July 23, 2021 Developers Solution Share Posted July 23, 2021 Something like this (Untested): #include <Constants.au3> #include <WinAPIDiag.au3> For $i = 1000 To 1 Step -1 If Check_Connected() Then ConsoleWrite($i & @CRLF) Sleep(1000) ; The variable $i will decrease by 1 unit every second EndIf Next Func Check_Connected() While 1 If _WinAPI_IsInternetConnected() Then Return true Else ConsoleWrite("Connection no exists" & @CRLF) Sleep(1000) EndIf WEnd EndFunc ;==>Check_Connected Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
dphuc23 Posted July 23, 2021 Author Share Posted July 23, 2021 2 minutes ago, Jos said: Something like this (Untested): #include <Constants.au3> #include <WinAPIDiag.au3> For $i = 1000 To 1 Step -1 If Check_Connected() Then ConsoleWrite($i & @CRLF) Sleep(1000) ; The variable $i will decrease by 1 unit every second EndIf Next Func Check_Connected() While 1 If _WinAPI_IsInternetConnected() Then Return true Else ConsoleWrite("Connection no exists" & @CRLF) Sleep(1000) EndIf WEnd EndFunc ;==>Check_Connected Jos Thanks. It worked perfectly. Also, I tried my code (below) but It doesn't seem to work very well 😅😅 Func Wait() Do ConsoleWrite("Connection no exists" & @CRLF) Until _WinAPI_IsInternetConnected() = True EndFunc Link to comment Share on other sites More sharing options...
JockoDundee Posted July 24, 2021 Share Posted July 24, 2021 Keeping your original code and adding one line: #include <Constants.au3> #include <WinAPIDiag.au3> For $i = 1000 To 1 Step -1 If _WinAPI_IsInternetConnected() Then ConsoleWrite($i & @CRLF) Sleep(1000) ; The variable $i will decrease by 1 unit every second Else ConsoleWrite("Connection no exists" & @CRLF) $i+=1 Sleep(1000) EndIf Next FrancescoDiMuro 1 Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
Dana Posted July 29, 2021 Share Posted July 29, 2021 Why not (untested): For $i = 1000 To 1 Step -1 While Not _WinAPI_IsInternetConnected() ConsoleWrite("Connection no exists" & @CRLF) Sleep(1000) WEnd ConsoleWrite($i & @CRLF) Sleep(1000) ; The variable $i will decrease by 1 unit every second Next Link to comment Share on other sites More sharing options...
JockoDundee Posted July 29, 2021 Share Posted July 29, 2021 1 hour ago, Dana said: Why not (untested): Because if it can’t connect after so many tries, he wants it to exit. Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now