I modified the sample code from the "Interrupting a running function" Wiki, and as soon as Ping is called, the GUI freezes for 15 seconds when the network cable is unplugged. The loop that starts after Ping is called doesn't begin until ping returns something, so the loop only runs once (because ping returns 0 when the cable is unplugged). #include <GUIConstantsEx.au3>
;Declare a flag
Global $fRunOne = False
Global $pingtest = 1234567890 ;just giving it a value here to see when it changes - a ping return will hopefully never be 1,234,567,890 milliseconds (two weeks!)
Opt("GUIOnEventMode", 1)
$hGUI = GUICreate("Test", 500, 500)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
$hButton_1 = GUICtrlCreateButton("Func One", 10, 10, 80, 30)
GUICtrlSetOnEvent($hButton_1, "_Func_1")
$hButton_2 = GUICtrlCreateButton("Func Two", 10, 50, 80, 30)
GUICtrlSetOnEvent($hButton_2, "_Func_2")
GUISetState()
While 1
Sleep(10)
;Check if the flag has been set by the OnEvent function
If $fRunOne Then
;Now start the "real" function from within the main code
_Func_1_Run()
EndIf
WEnd
Func _Func_1()
; Set the flag within the OnEvent function
Global $fRunOne = True
EndFunc ;==>_Func_1
Func _Func_1_Run()
ConsoleWrite(@CRLF & "$pingtest value before ping = " & $pingtest & @CRLF)
$pingtest = Ping("www.google.com",3000)
For $i = 1 To 6 ;loop runs every .5 seconds, need 3-second limit
If $fRunOne Then
ConsoleWrite("Func_1_Run is running, loop " & $i & @CRLF)
ConsoleWrite("$pingtest valuefor loop " & $i & " = " & $pingtest & @CRLF)
If $pingtest <> 1234597890 Then
ConsoleWrite("exiting loop" & @CRLF)
ExitLoop
EndIf
Sleep(500)
EndIf
Next
ConsoleWrite(">Func 1 Ended" & @CRLF)
Global $fRunOne = False
EndFunc ;==>_Func_1_Run
Func _Func_2()
;~ For $i = 1 To 3
Global $fRunOne = False
ConsoleWrite("+Func 2 Running, ending _Func_1_Run" & @CRLF)
;~ Sleep(100)
;~ Next
ConsoleWrite(">Func 2 Ended" & @CRLF)
EndFunc ;==>_Func_2
Func _Exit()
Exit
EndFunc ;==>_Exit