Jump to content

Pause script if No / lost Internet connection


DigDeep
 Share

Recommended Posts

Hi,

I have a too big code which runs in Process 24/7 looking for any issues with multiple applications on the machine. If any application is not running or found bad, it tries to get them fixed. My issue is that I want my code to PAUSE if Internet connection is not there and as soon as the internet connection comes back it should RESUME.

While _WinAPI_IsInternetConnected()
    Looks for issues...
        Looks for issues...
            Looks for issues...
        Looks for issues...
    Looks for issues...
Continueloop
Wend

I am able to do my work by keeping the  _WinAPI_IsInternetConnected()  just before "Looks for issues" but then I have to keep adding this before every Function...

If I want to keep it as above, then the code exits if no Internet connection found. Is there any way I can keep this to at the starting so it will keep checking for an Active Internet connection all the time and until found it will pause the code Or not take any actions inside While.

Also, I am not showing the icon in System Tray, so I think using Pause function via TrayItemSetState might not work.

Thanks in advance.

 

Edited by DigDeep
Link to comment
Share on other sites

Try this (Code not tested):

#include <WinAPIDiag.au3>

AdlibRegister("checkconnection",250)

;Your code here ,,,,,,,,,,,
;.,,,,,,,,,,,,,,,,


func checkconnection()
Do
sleep(50)
until _WinAPI_IsInternetConnected()
endfunc

I dont know wether that works anyway test it

No matter whatever the challenge maybe control on the outcome its on you its always have been.

MY UDF: Transpond UDF (Sent vriables to Programs) , Utter UDF (Speech Recognition)

Link to comment
Share on other sites

Another way :

While 1
     If _WinAPI_IsInternetConnected() Then
        Looks for issues...
            Looks for issues...
                Looks for issues...
            Looks for issues...
        Looks for issues...
     Else
        Sleep(1000)
     EndIf
WEnd

 

Link to comment
Share on other sites

You said I want my code to PAUSE if Internet connection is not there and as soon as the internet connection comes back it should RESUM.

So how can you detect if the connection comes back ? Not with a blocking function, sure.

With my code, if the internet connection is down, the loop continues and check it after 1 second. If it is OK, the issues check performs. Else, waits for 1 second and re-check...

 

Link to comment
Share on other sites

Another way :

While 1
     If _WinAPI_IsInternetConnected() Then
        Looks for issues...
            Looks for issues...
                Looks for issues...
            Looks for issues...
        Looks for issues...
     Else
        Sleep(1000)
     EndIf
WEnd

 

in that code if the internet is connected then the If command will satisfy and pass on to the "look for issues" but while on the "look for issues" if the internet is lost the program will still continue with "look for issues" without internet i will demonstrate with an example:

While 1
     If _WinAPI_IsInternetConnected() Then ;when connection is there if satisfies 
        sleep(5000)
        ConsoleWrite("Connection still exists") ;here even if the connection is terminated the script continues
     Else
        Sleep(1000)
     EndIf
WEnd

but if you use adlibregister it will poll for internet connection.

the function registered via adlibregister wont block the script unless not connected to the internet if you want to have a larger poll interval you could use a larger function call interval in adlibregister

Edited by Surya

No matter whatever the challenge maybe control on the outcome its on you its always have been.

MY UDF: Transpond UDF (Sent vriables to Programs) , Utter UDF (Speech Recognition)

Link to comment
Share on other sites

@jguinch

Well, this works. But the issue found that if internet gets disconnected while fixing the issue the code still continues (which is good) but when the Internet connection is back, the code does not resume.

Say, the issue found was at the 2nd Looks for issues. I found that if Internet is disconnected, and resumed the code does not restart. It still runs at the Process, but does not do anything... What I want is, if Internet gets disconnected also in between if any actions are being done and then Internet is back, the code should restart from While 1 again, identify the issue and restart the fix.

While 1
     If _WinAPI_IsInternetConnected() Then
        Looks for issues...
            Looks for issues...
Edited by DigDeep
Link to comment
Share on other sites

Ok, I understand.

Something like this should be simpler

While 1
    Sleep(1000)
    If NOT _WinAPI_IsInternetConnected() Then ContinueLoop
    Process First issues check
    
    If NOT _WinAPI_IsInternetConnected() Then ContinueLoop
    Process Second issues check
    
    If NOT _WinAPI_IsInternetConnected() Then ContinueLoop
    Process Third issues check      
    
    ; End
    ExitLoop
WEnd

 

Link to comment
Share on other sites

@jguinch

Both of your codes works fine. But one of the issue mentioned above still persists.

While fixing is in process, if Internet turns OFF and fixing stops, then the file shows as running in Process, but it just sits like that. It no longer checks for issues in loop. I have to either exit the Process and restart it or restart the machine which I don't want.

1. If No Internet connection then continue loop to wait for connection.

2. If Active Internet connection then run the functions.

3. If Internet is interrupted then it should go back to loop state.

4., When Internet is back ON, it should start running functions.

Also I have removed the ExitLoop as it was killing the process if no issues were found.

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