Jump to content

Pause For loop when lost connection


Go to solution Solved by Jos,

Recommended Posts

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

  • Developers
  • Solution

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

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

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

 

Code hard, but don’t hard code...

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...