Jump to content

Ping timeout


Recommended Posts

Anyone aware of a reason that the following code would take 15 seconds (rather than two seconds) to display the MsgBox when I unplug my network cable? Also, @error is 0 even though ping failed. Hmmm...

$PingTime = Ping("www.google.com",2000)

MsgBox(0, "", "$PingTime = " & $PingTime)

MsgBox(0, "", "@error = " & @error)

This thread ended with basically the same question a while back:

Link to comment
Share on other sites

Because it's a timout on latency, say the pinged website takes less than two seconds to respond, but their both shaking hands over and over in under 2 seconds each handshake, it will take long unless one of the "handshakes" took longer than 2 seconds i think

As for the other thing, I don't know

Edited by ApudAngelorum
Link to comment
Share on other sites

OK, but there are no handshakes going on at all when my network cable is unplugged...?

Hmmm, true...

MAybe someone with more exp can explain what's happening. As for me, I tend to avoid using the ping function when dealing with online stuff, I like using the winhttp.au3 udf and just try connecting to the site using that as a sort of ping since it's a little more controllable, especially when you do the pinging from another process and just close it if the time exceeded.

There are also various functions to check if the network is alive and if the computer has internet access, winapiex.au3 has that functionality for vista and above

Link to comment
Share on other sites

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
Link to comment
Share on other sites

Regarding the responsiveness of your GUI, the best way I know that you can keep your UI responsive is to use multi processing together with registering a callback for WM_COMMAND.

When creating your ui, make sure everything is set to on event mode, functionality that should remain responsive should be called from the WM_COMMAND call back, functions that are called from the wm_command that will take a long time should be called using adlibregister and unregister the function once it was called, this way the callback remains uninterrupted.

Anything dealing with TCP, UDP, pinging or connecting to the internet should be done using multiprocessing, create a header that will detect some command line arguments and have it do what needs to be done, this way your ui will always remain responsive.

Link to comment
Share on other sites

I didn't get back about this earlier, partly because I was busy but also because my internet connection has been a bit unreliable lately..I didn't want to be swirtching it on and off to test code. However after a quick look at your code, I immediately notice one error: You shouldn't use the Global keyword inside functions. Instead use Local. I would still like to know what this is for.

Link to comment
Share on other sites

This inconvience may be because of your system environment or other factors. Unless you have AV running I would suggest doing a scan. This could also be because of your network activity, open the command prompt and use "netstat/?" to display the command's usage.

Edited by Decipher
Spoiler

censored.jpg

 

Link to comment
Share on other sites

  • 7 months later...

I have the same problem - it also takes exactly 15 seconds.

I'm using it inside a LAN. If a host is online, then the result is returned almost instantly.

If it is not then the ping command takes 15 seconds to finish and let autoit move to the next line to the if statement telling it what msgbox to display.

This is regardless of the timeout I set (you'll notice it's set to 1).

$Ping = Ping($Hostname,1)
If $Ping Then
MsgBox(0,"Ping request to " & $Hostname,"A ping request was sent to... " & $Hostname & " ...network connectivity was established.")
Call("Func2")
Else
Msgbox(0,"Status","An error occured connecting to " & $Hostname & ". Check network connectivity and firewall ...will now quit.")
Exit
EndIf
Edited by SuperFletch
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...