Jump to content

ping loop help


chewy
 Share

Recommended Posts

I know this should probably be basic programming but I am having an issue with this. I am trying to create a simple app that pings a server to tell if it is up or not. if it is up it puts a checkbox image and if is not reachable it is given an x image. My code seems to work fine but I cant get the window to close when I click the close "x". It seems like it is too busy to close it or something. Sorry for the probably noob question.

#include <INet.au3>
#include <GUIConstantsEx.au3>

GUICreate("Hello World", 300, 300) 


$buttonServer1 = GuiCtrlCreatePic("C:\Users\Administrator\Desktop\ping server test\Exclamation.bmp",110,0, 0,0)


GUISetState()


$i = 0
While 1
    $var = Ping("www.google.com")
    If $var Then; also possible:  If @error = 0 Then ...

  
        GUICtrlSetImage($buttonServer1, "C:\Users\Administrator\Desktop\ping server test\CheckMark.bmp")
        GUISetState()

            $i = $i + 1
            Sleep(10000) ;two seconds

    Else
       
        GUICtrlSetImage($ButtonServer1, "C:\Users\Administrator\Desktop\ping server test\Failed.bmp")
        GUISetState()

            $i = $i + 1
            Sleep(10000) ;two seconds

    EndIf
    

WEnd
Link to comment
Share on other sites

help file:

Ping ( "address/hostname" [, timeout] )

its better set timeout, to minimize delay!

this code from somewhere in forum

ConsoleWrite(_ping("http:\\www.google.com")); test 1


Func _ping($host)
    $var = Ping($host,250)
    If $var Then  Return $var & "/ms"& @CRLF
    Switch @error
        Case 1 
            Return "Host is offline" & @CRLF
        Case 2 
            Return "Host is unreachable"& @CRLF
        Case 3 
            Return "Bad destination"& @CRLF
        Case 4 
            Return "Other errors"& @CRLF
    EndSwitch
EndFunc

btw, this one what i use to checking connection, maybe can help

#Region _isconnect2host
Func _isconnect2host($svHost, $ivSleepTime = 250)
InetGet($svHost, @TempDir&"\foo.html",1,1)
While @InetGetActive
    ConsoleWrite("Downloading: Bytes = " & @InetGetBytesRead & @CRLF)
        If @InetGetBytesRead > 0 Then
            Return True
        EndIf
    Sleep($ivSleepTime)
Wend 
    Return False
EndFunc
#EndRegion
Edited by rvn
Link to comment
Share on other sites

help file:

Ping ( "address/hostname" [, timeout] )

its better set timeout, to minimize delay!

i did put that in but it had no effect on being able to close the window.

im on a win 7 pro machine if that makes a difference

Link to comment
Share on other sites

Instead of using sleep(x) use something like this instead

$Timer = TimerInit)
While 1
    $var = Ping("www.google.com")
    $Delay = TimerDiff($timer)
    If $Delay >= 2000 Then  ;2 seconds delay check
        If $var Then; also possible:  If @error = 0 Then ...
           GUICtrlSetImage($buttonServer1, "C:\Users\Administrator\Desktop\ping server test\CheckMark.bmp")
           GUISetState()
           $i = $i + 1
        Else
          GUICtrlSetImage($ButtonServer1, "C:\Users\Administrator\Desktop\ping server test\Failed.bmp")
          GUISetState()
          $i = $i + 1
        EndIf
        $Timer = TimerInit()
   Endif
WEnd
Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Instead of using sleep(x) use something like this instead

$Timer = TimerInit)
While 1
    $var = Ping("www.google.com")
    $Delay = TimerDiff($timer)
    If $Delay >= 2000 Then  ;2 seconds delay check
        If $var Then; also possible:  If @error = 0 Then ...
           GUICtrlSetImage($buttonServer1, "C:\Users\Administrator\Desktop\ping server test\CheckMark.bmp")
           GUISetState()
           $i = $i + 1
        Else
          GUICtrlSetImage($ButtonServer1, "C:\Users\Administrator\Desktop\ping server test\Failed.bmp")
          GUISetState()
          $i = $i + 1
        EndIf
        $Timer = TimerInit()
   Endif
WEnd

this does the trick thanks! it does run the processor a little high but not a deal breaker.

Link to comment
Share on other sites

Change this :

Endif
Wend

To This:

Endif
    Sleep(10)
Wend

It will decrease the CPU load immensely and still let you break out of it using the onevent function calls.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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