Jump to content

Pause script if network is disabled, resume when enabled


Recommended Posts

Hello all,

I need some help with a process that runs during my main script execution to check the status of the network connection.  If the network connection becomes disabled, a GUI message is displayed and the script should pause until the network connection becomes enabled again.  I am able to successfully pause the script but once the network connection becomes enabled, the gui window does not dissapear.  My main script uses "GUIOnEventMode" and the network check process is being executed by the AdlibRegister command.

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

AdlibRegister("_CheckNetworkState")
Opt("GUIOnEventMode", 1)
Opt("TrayAutoPause", 0)
Global $CheckingForOffline = True
Global $Connected = ""
$Time = 0

#Region ### START Koda GUI section ### Form=c:\users\heisenberg\desktop\networkaliveguiform1.kxf
$NetworkAliveGUIForm1 = GUICreate("",(@DesktopWidth+50), (@DesktopHeight), -1, -1, $WS_POPUP, BitOR($WS_EX_TOPMOST,$WS_EX_WINDOWEDGE))
GUISetBkColor(0xFF0000)
$NetworkAliveLabel1 = GUICtrlCreateLabel("*** WARNING ***", 0, 20, @DesktopWidth+50, 150, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 72, 800, 0, "Calibri")
GUICtrlSetColor(-1, 0x000000)
GUICtrlSetBkColor(-1, 0xC0C0C0)
$NetworkAliveLabel2 = GUICtrlCreateLabel("message...blah, blah, blah", 0, 172, @DesktopWidth, 125, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 72, 800, 0, "Calibri")
GUICtrlSetColor(-1, 0x000000)
$NetworkAliveLabel3 = GUICtrlCreateLabel("Network connectivity status:", 0, 300, @DesktopWidth, 125, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 72, 800, 0, "Calibri")
GUICtrlSetColor(-1, 0x000000)
$NetworkAliveLabel4 = GUICtrlCreateLabel("disconnected", 0, 428, @DesktopWidth+50, 125, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 72, 800, 0, "Calibri")
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0x000000)
$NetworkAliveLabel5 = GUICtrlCreateLabel('This message will be displayed until the' & @CRLF & 'network status changes to "Connected".', 0, 564, @DesktopWidth, 250, $SS_CENTER)
GUICtrlSetFont(-1, 48, 800, 0, "Calibri")
GUICtrlSetColor(-1, 0x000000)
GUISetState(@SW_HIDE)
#EndRegion ### END Koda GUI section ##

ConsoleWrite("Result:: " & _NetworkActive() & @CRLF)

_GetInitialState()

#Region --- CodeWizard generated code Start ---
;SpashText features: Title=Yes, Text=Yes, Width=300, Height=75, Always On Top, Windows can be moved, Left justified text, Fontname=Calibri, Font size=20, Font weight=700
$Splash = SplashTextOn("Test...","Seconds: "& $Time,"300","75","-1","-1",20,"Calibri","20","700")
#EndRegion --- CodeWizard generated code End ---

;Test looping to check main script running

Do
    SoundPlay(@WindowsDir & "\media\ding.wav")
    ControlSetText("Test...", "", "Static1", "Seconds: "& $Time)
    $Time = $Time+1
    Sleep(1000)
Until $Time = 31
SplashOff()
MsgBox(0,"","END")

Exit

Func _NetworkActive()
    Local $sz_url = "http://www.google.com"
    Local $u_flags = 0x00000001
    Local $u_reserved = 0
    Local $winnet = DllOpen("WinInet.dll")
    Local $Rsut = DllCall($winnet, "BOOL", "InternetCheckConnection", "str", $sz_url, "dword", $u_flags, "dword", $u_reserved)
    Return ($Rsut[0] <> 0)
    DllClose($winnet)
EndFunc

Func _GetInitialState()
    Local $IsConnected = _ConnectionStatus()
    If @error Then
        Return
    Else
        If $IsConnected Then
            $CheckingForOffline = True
            ConsoleWrite('-> checking to see if the network status is disconnected...' & @CRLF & @CRLF)
        Else
            $CheckingForOffline = False
            ConsoleWrite('>> checking to see if the network status is active...' & @CRLF & @CRLF)
        EndIf
    EndIf
EndFunc

Func _CheckNetworkState()
    Local $IsConnected = _ConnectionStatus()
    If @error Then
        Return
    Else
        If $CheckingForOffline Then
            If NOT $IsConnected Then
                ConsoleWrite('!> Netowrk is disconnected' & @CRLF)
                GUISetState(@SW_SHOW, $NetworkAliveGUIForm1)
                $CheckingForOffline = False
                Pause()
                ConsoleWrite('>> Network reconnection started...' & @CRLF & @CRLF)
            EndIf
        Else
            If $IsConnected Then
                ConsoleWrite('+> Network has been reconnected' & @CRLF)
                GUISetState(@SW_HIDE, $NetworkAliveGUIForm1)
                $CheckingForOffline = True
                ConsoleWrite('-> checking to see if the network status is disconnected...' & @CRLF & @CRLF)
            EndIf
        EndIf
    EndIf
EndFunc

Func _ConnectionStatus()
    Local $aDllRet = DllCall("wininet.dll", "int", "InternetGetConnectedState", "long_ptr", 0, "long", 0)
    If @error OR NOT IsArray($aDllRet) then Return SetError(1, 0, 0)
    If $aDllRet[0] > 0 Then
        $Connected = True
        Return True
    Else
        Return False
    EndIf
EndFunc

Func Pause()
    If WinActive($NetworkAliveGUIForm1) Then
        WinWaitClose($NetworkAliveGUIForm1)
    EndIf
EndFunc

Any help would be greately appriciated! :D

Credit...most of the above script was originall created by dragon from topic "How can stop program if IE was diconnected".  Thanks dragon!

Link to comment
Share on other sites

Try eg:

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
;~ Global Const $SS_CENTER = 0x1
;~ Global Const $SS_CENTERIMAGE = 0x0200
;~ Global Const $WS_DLGFRAME = 0x00400000
;~ Global Const $WS_POPUP = 0x80000000
;~ Global Const $WS_EX_TOPMOST = 0x00000008
Opt("TrayAutoPause", 0)
#Region # START GUI
Global $hGUI = GUICreate("Form1", @DesktopWidth, 82, -1, -1, BitOR($WS_DLGFRAME, $WS_POPUP), $WS_EX_TOPMOST)
GUICtrlCreateLabel("*** WARNING ***", 0, 0, 412, 75, BitOR($SS_CENTER, $SS_CENTERIMAGE))
GUICtrlSetFont(-1, 40, 400, 0, "Segoe UI")
GUICtrlSetColor(-1, 0x000000)
GUICtrlSetBkColor(-1, 0xFF0000)
GUICtrlCreateLabel('This message will be displayed until the network status changes to "Connected".', 424, 48, 832, 25)
GUICtrlSetFont(-1, 12, 800, 0, "Segoe UI")
GUICtrlSetColor(-1, 0x000000)
GUICtrlCreateLabel("Network connectivity status:", 424, 8, 286, 34)
GUICtrlSetFont(-1, 16, 800, 0, "Segoe UI")
GUICtrlSetColor(-1, 0x000000)
GUICtrlCreateLabel("Disconnected", 720, 8, 530, 34)
GUICtrlSetFont(-1, 16, 800, 0, "Segoe UI")
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0x000000)
GUISetState(@SW_HIDE)

#EndRegion # START GUI
Global $netStatus = 0

Local $sTime
While 1
    If Not _CheckConnect() Then
        GUISetState(@SW_SHOW, $hGUI)
        $sTime = 0
        Sleep(10)
    Else
        GUISetState(@SW_HIDE, $hGUI)
        SoundPlay(@WindowsDir & "\media\ding.wav")
        $sTime += 1
        SplashTextOn("Test...", " Seconds: " & $sTime, 300, 75, -1, -1, 20, "Segoe UI", 20, 700)
        Sleep(1000)
    EndIf
WEnd

Func _CheckConnect()
    If _Connected() Then
        If $netStatus <> 2 Then
            TrayTip("", "Connection Established", 4, 1)
            TraySetToolTip("Connection Established")
            $netStatus = 2
        EndIf
        Return 1
    Else
        If $netStatus <> 1 Then
            TrayTip("Warning", "Connection Lost", 4, 2)
            TraySetToolTip("Connection Lost")
            $netStatus = 1
        EndIf
        Return 0
    EndIf
EndFunc   ;==>_CheckConnect
;==>_Connected
Func _Connected()
    Local $Status = DllCall("WinInet.dll", "int", "InternetGetConnectedState", "int_ptr", 0, "int", 0)
    Return SetError(@error, @extended, $Status[0])
EndFunc   ;==>_Connected

 

Edited by Trong
reduce CPU

Regards,
 

Link to comment
Share on other sites

There's this function

_WinAPI_IsInternetConnected
#include <WinAPIDiag.au3>
#include <GUIConstants.au3>

AutoItSetOption("GuiOnEventMode", 1)

Global $hMain = GUICreate("Title")

GUISetState(@SW_SHOW, $hMain)
GUISetOnEvent($GUI_EVENT_CLOSE, Close)

AdlibRegister(CheckNetwork, 1000)

While (True)
    Sleep(100)
WEnd

Func CheckNetwork()
    If (_WinAPI_IsInternetConnected()) Then Return
    AdlibUnRegister(CheckNetwork)
    Local $bOnEventRet = AutoItSetOption("GuiOnEventMode", 0)
    Local $hNetwork = GUICreate("Network", 200, 30)
    Local $lblNetwork = GUICtrlCreateLabel("Network is currently unavailable", 10, 10)

    GUISetState(@SW_DISABLE, $hMain)
    GUISetState(@SW_SHOW, $hNetwork)

    While (Not _WinAPI_IsInternetConnected())
        Switch (GUIGetMsg())
            Case $GUI_EVENT_CLOSE
                GUIDelete($hNetwork)
                GUIDelete($hMain)
                Exit 0
        EndSwitch
    WEnd

    GUIDelete($hNetwork)
    GUISetState(@SW_ENABLE, $hMain)
    AdlibRegister(CheckNetwork, 1000)
    AutoItSetOption("GuiOnEventMode", $bOnEventRet)
EndFunc

Func Close()
    GUIDelete($hMain)
    Exit 0
EndFunc

 

Edited by InunoTaishou
Link to comment
Share on other sites

38 minutes ago, Trong said:

Try eg:

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
;~ Global Const $SS_CENTER = 0x1
;~ Global Const $SS_CENTERIMAGE = 0x0200
;~ Global Const $WS_DLGFRAME = 0x00400000
;~ Global Const $WS_POPUP = 0x80000000
;~ Global Const $WS_EX_TOPMOST = 0x00000008
Opt("TrayAutoPause", 0)
#Region # START GUI
Global $hGUI = GUICreate("Form1", @DesktopWidth, 82, -1, -1, BitOR($WS_DLGFRAME, $WS_POPUP), $WS_EX_TOPMOST)
GUICtrlCreateLabel("*** WARNING ***", 0, 0, 412, 75, BitOR($SS_CENTER, $SS_CENTERIMAGE))
GUICtrlSetFont(-1, 40, 400, 0, "Segoe UI")
GUICtrlSetColor(-1, 0x000000)
GUICtrlSetBkColor(-1, 0xFF0000)
GUICtrlCreateLabel('This message will be displayed until the network status changes to "Connected".', 424, 48, 832, 25)
GUICtrlSetFont(-1, 12, 800, 0, "Segoe UI")
GUICtrlSetColor(-1, 0x000000)
GUICtrlCreateLabel("Network connectivity status:", 424, 8, 286, 34)
GUICtrlSetFont(-1, 16, 800, 0, "Segoe UI")
GUICtrlSetColor(-1, 0x000000)
GUICtrlCreateLabel("Disconnected", 720, 8, 530, 34)
GUICtrlSetFont(-1, 16, 800, 0, "Segoe UI")
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0x000000)
GUISetState(@SW_HIDE)

#EndRegion # START GUI
Global $netStatus = 0

Local $sTime
While 1
    If Not _CheckConnect() Then
        GUISetState(@SW_SHOW, $hGUI)
        $sTime = 0
        Sleep(10)
    Else
        GUISetState(@SW_HIDE, $hGUI)
        SoundPlay(@WindowsDir & "\media\ding.wav")
        $sTime += 1
        SplashTextOn("Test...", " Seconds: " & $sTime, 300, 75, -1, -1, 20, "Segoe UI", 20, 700)
        Sleep(1000)
    EndIf
WEnd

Func _CheckConnect()
    If _Connected() Then
        If $netStatus <> 2 Then
            TrayTip("", "Connection Established", 4, 1)
            TraySetToolTip("Connection Established")
            $netStatus = 2
        EndIf
        Return 1
    Else
        If $netStatus <> 1 Then
            TrayTip("Warning", "Connection Lost", 4, 2)
            TraySetToolTip("Connection Lost")
            $netStatus = 1
        EndIf
        Return 0
    EndIf
EndFunc   ;==>_CheckConnect
;==>_Connected
Func _Connected()
    Local $Status = DllCall("WinInet.dll", "int", "InternetGetConnectedState", "int_ptr", 0, "int", 0)
    Return SetError(@error, @extended, $Status[0])
EndFunc   ;==>_Connected

 

Looks good!  Thank you.  :)

Link to comment
Share on other sites

31 minutes ago, InunoTaishou said:

There's this function

_WinAPI_IsInternetConnected
#include <WinAPIDiag.au3>
#include <GUIConstants.au3>

AutoItSetOption("GuiOnEventMode", 1)

Global $hMain = GUICreate("Title")

GUISetState(@SW_SHOW, $hMain)
GUISetOnEvent($GUI_EVENT_CLOSE, Close)

AdlibRegister(CheckNetwork, 1000)

While (True)
    Sleep(100)
WEnd

Func CheckNetwork()
    If (_WinAPI_IsInternetConnected()) Then Return
    AdlibUnRegister(CheckNetwork)
    Local $bOnEventRet = AutoItSetOption("GuiOnEventMode", 0)
    Local $hNetwork = GUICreate("Network", 200, 30)
    Local $lblNetwork = GUICtrlCreateLabel("Network is currently unavailable", 10, 10)

    GUISetState(@SW_DISABLE, $hMain)
    GUISetState(@SW_SHOW, $hNetwork)

    While (Not _WinAPI_IsInternetConnected())
        Switch (GUIGetMsg())
            Case $GUI_EVENT_CLOSE
                GUIDelete($hNetwork)
                GUIDelete($hMain)
                Exit 0
        EndSwitch
    WEnd

    GUIDelete($hNetwork)
    GUISetState(@SW_ENABLE, $hMain)
    AdlibRegister(CheckNetwork, 1000)
    AutoItSetOption("GuiOnEventMode", $bOnEventRet)
EndFunc

Func Close()
    GUIDelete($hMain)
    Exit 0
EndFunc

 

Thank you! I missed that command.  :> 

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