Sign in to follow this  
Followers 0
Ruud0209

If ping OK do this else that (2)

1 post in this topic

In my first post I wrote I wanted to make an application that pings a host with an interval.

Since then, with the help of Lod3n and numerous forum contributors, I've been able to make a little program that actually works fine except that it now pings a little to often (i think)

The problem, I think, is in the last "while" loop.

I want it to ping the host but I also want it to check the system tray for input . When I put a sleep in the first part it seems like it neer sleeps and when i put it after the second part (after the select) in only sleeps.

Whoever is willing to take a good look and give me some hints. I also would like some "good programming conduct" feedback if possible if you know what I mean.

Search for ;;;;- to get to the things I have a question about.

What the application does is check if it is installed, checks what IP the PC it runs on has, if the ini should be read and then displays a IE object/page.

The original ping loop was kindly provided by lod3n.

If you intend to run it then remember there may be some registrykeys written (if you install):

HKLM\Software\Zuilen

HKCU\Software\Zuilen

HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\Zuilen

and

HKCU\Software\Microsoft\Windows\Currentversion\Run key: Zuilen

Any help appreciated.

I will insert my code here :

;; Includes

#Include <GUIConstants.au3>
;#Include <GUIDefaultConstants.au3>
;#Include <Constants.au3>
;#Include <WindowsConstants.au3>
#Include <IE.au3>

Global $autostartitem
Global $lokatie
Global $inifile
Global $initem
; ----------------------------------------------------------------------------


;;;; Funtioncs

Func _SelectLok()


      $Form2 = GUICreate("Vestiging_selectie", 240, 160, @DesktopWidth / 2 - 120, @DesktopHeight / 2 - 260)
    $City_Combo = GUICtrlCreateCombo("Eindhoven", 24, 48, 177, 21)
    GUICtrlSetData(-1, "Eindhoven|Nijmegen|Heerlen|Maastricht|Roermond|Venlo|Sittard|Helmond")
    GUICtrlCreateLabel("Welke vestiging wilt u emuleren ?", 24, 24, 162, 17)
    $Group1 = GUICtrlCreateGroup("Maak uw keuze:", 8, 8, 224, 140)
    

    $OKButton = GUICtrlCreateButton("&OK",14, 100, 210, 33 ) 
    GUICtrlSetState(-1,$GUI_FOCUS)  
    GUISetState(@SW_SHOW)



while 1
        $msg1 = GuiGetMsg()
        Select
            Case $msg1 = $GUI_EVENT_CLOSE
                GUISetState(@SW_HIDE)
                terminate()
                Exitloop
            Case $msg1 = $OKButton
                GUISetState(@SW_HIDE)
                $selection = GUICtrlRead( $city_Combo )
                Return $selection

                Case Else
            ;;;;;
        EndSelect
Wend

EndFunc
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Func terminate()
    ConsoleWrite('@@ (107) :(' & @MIN & ':' & @SEC & ') terminate()' & @CR);### Trace Function
    exit 3
EndFunc
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


Func _Install()
    
;If RegRead("HKEY_LOCAL_MACHINE\Software\Zuilen","InstallPath") Then
;;  MsgBox(64, "Error" , "errorlevel : " & @error )
;Else
;   MsgBox(64, "Error" , "errorlevel : " & @error )
    $PathToInstall = FileSelectFolder("Select Folder", @ProgramFilesDir ,3)
    RegWrite("HKEY_LOCAL_MACHINE\Software\Zuilen","Installpath","REG_SZ", $PathToInstall)
    RegWrite("HKEY_LOCAL_MACHINE\Software\Zuilen","ProgramName","REG_SZ", @ScriptName)
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Zuilen", "DisplayIcon", "REG_SZ", $PathToInstall & "\" & @ScriptName )
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Zuilen", "DisplayIconEx", "REG_SZ", $PathToInstall & "\" & @ScriptName )
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Zuilen", "DisplayName", "REG_SZ", "Zuilen Emulator")
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Zuilen", "EstimatedSizeEx", "REG_DWORD", "00000259")
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Zuilen", "InstallLocationEx", "REG_SZ", $PathToInstall)
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Zuilen", "NoModify", "REG_DWORD", "00000001")
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Zuilen", "NoRepair", "REG_DWORD", "00000001")
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Zuilen", "UninstallString", "REG_SZ", """" &  $PathToInstall & "\uninstall.exe" & """" )
    RegWrite("HKEY_CURRENT_USER\Software\Zuilen","PingSpeed","REG_SZ", "250")
    RegWrite("HKEY_CURRENT_USER\Software\Zuilen","ReadIni","REG_SZ", "0")

    FileInstall ("C:\Program Files\AutoIt3\Projects\zuilen\zuilen_banner.jpg" , $PathToInstall & "\", 1 )
    DirCreate ( @WindowsDir & "\uninstall\zuilen")
    FileInstall ("C:\Program Files\AutoIt3\Projects\zuilen\uninstall.exe" , $PathToInstall & "\",1)
    FileCopy( @ScriptFullPath , $PathToInstall , 1 )
    DirCreate ( @ProgramsCommonDir & "\Zuilen")
    FileCreateShortcut( $PathToInstall & "\" & @ScriptName, @ProgramsCommonDir & "\Zuilen\Zuilen Emulator.lnk")
    return
    Exit
    Exit
;EndIf
    
EndFunc

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Func OnAutoItStart()

    If $CmdLine[0]>"0" Then
        If $CmdLineRaw <> "" Then
        ;;
            IF $CmdLineRaw = "/INSTALL" Then
                _Install()
                exit
            Else
                MsgBox(16, "De parameter is incorrect !", "Gebruik : " & @ScriptName & " [/INSTALL]")
                Exit
            Endif
        Endif
    Endif
EndFunc


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Func _SetPingValue()
    
;#include <GUIConstants.au3>
; == GUI generated with Koda ==
Local $Form2
Local $Group2
Local $Input2
Local $Slider2
Local $msg2
Local $Pingvalue
$Pingvalue = RegRead("HKEY_CURRENT_USER\Software\Zuilen","PingSpeed")
$Form2= GUICreate("Stel ping waarde in millisecondes in :", 533, 183, 192, 125)
$Group2 = GUICtrlCreateGroup("Stel de ping waarde in met de schuifknop", 8, 8, 513, 161)
$Input2 = GUICtrlCreateInput("$Pingvalue", 32, 32, 89, 21)
$Slider2 = GUICtrlCreateSlider(144, 32, 353, 41, BitOR($TBS_AUTOTICKS,$TBS_NOTICKS))
GUICtrlSetLimit(-1,1500,250)
GUICtrlSetData( $Slider2 , $Pingvalue )
GUICtrlCreateLabel("250", 144, 72, 22, 17)
GUICtrlCreateLabel("1500", 472, 72, 28, 17)
$Button2 = GUICtrlCreateButton("&OK", 176, 112, 145, 33, $BS_DEFPUSHBUTTON)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
While 1
    $msg2 = GUIGetMsg()
    GUICtrlSetData($Input2,GUICtrlRead($Slider2))
    Select
    Case $msg2 = $GUI_EVENT_CLOSE 
        Exit
        
    Case $msg2 = $Button2
        RegWrite("HKEY_CURRENT_USER\Software\Zuilen","PingSpeed","REG_SZ",GUICtrlRead($Slider2))
        GUISetState(@SW_HIDE)
        Return GUICtrlRead($Slider2)
        ExitLoop
    EndSelect
    sleep (25)
Wend

EndFunc

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func AutoStart()
    If RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Zuilen","InstallPath") = "" Then
        MsgBox ( 16, "Programma is niet geinstalleerd", "Installeer a.u.b. het programma eerst.",10)
    Else
        IF RegRead("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run","Zuilen") = "" Then
            If RegWrite("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run","Zuilen","REG_SZ",RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Zuilen","InstallPath") & "\" & RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Zuilen","ProgramName")) Then TrayItemSetState($autostartitem,65)
        Else
            If RegDelete("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run","Zuilen") Then TrayItemSetState($autostartitem,68)
        Endif
EndIf
EndFunc
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func _SetState()
If RegRead("HKEY_LOCAL_MACHINE\Software\Zuilen","Installpath") Then
    TrayItemSetState ($installitem,128)
    Else
    TrayItemSetText( $installitem,"Installeren")
Endif
EndFunc
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Func _SetIni()
    If RegRead("HKEY_LOCAL_MACHINE\Software\Zuilen","Installpath") Then
        If RegRead("HKEY_CURRENT_USER\Software\Zuilen","ReadIni") = "0" Then
            TrayItemSetState ($initem,65)
            RegWrite("HKEY_CURRENT_USER\Software\Zuilen","ReadIni","REG_SZ","1")
            return 1
        Else
            TrayItemSetState( $initem,68)
            RegWrite("HKEY_CURRENT_USER\Software\Zuilen","ReadIni","REG_SZ","0")
            return 0
        Endif
    Else
    MsgBox ( 16, "Programma is niet geinstalleerd", "Installeer a.u.b. het programma eerst.",10)
    EndIf   
EndFunc

Func _ReadIni()
    If RegRead("HKEY_LOCAL_MACHINE\Software\Zuilen","Installpath") Then
        If RegRead("HKEY_CURRENT_USER\Software\Zuilen","ReadIni") = "0" Then
            TrayItemSetState ($initem,68)
            return 0
        Else
            TrayItemSetState( $initem,65)
            return 1
        Endif
    Else
    MsgBox ( 16, "Programma is niet geinstalleerd", "Installeer a.u.b. het programma eerst.",10)
    return 0
    EndIf   
EndFunc


;;;; End Functions


;; Options
Opt("GUIOnEventMode", 0) ; Change to OnEvent mode
Opt("TrayMenuMode",1); Default tray menu items (Script Paused/Exit) will not be shown.
Opt("TrayIconDebug",0); Debug in tooltip
Opt("TrayAutoPause",0)
;; End Options

;; TrayTip
TrayTip ("Google Zuilen", "Het Zuilen programma is aktief" , 1, 1)
;; End Traytip


;;splashimage
#Region --- CodeWizard generated code Start ---
;SpashImage features: Title=Yes, Always On Top
SplashImageOn("Google Groep NV","zuilen_banner.jpg","480","60","-1","-1",1)
Sleep ( 1000 )
SplashOff()
#EndRegion --- CodeWizard generated code End ---

;;end splashimage
;; TrayItems




;;; Check Installation

;;;

$settingsitem = TrayCreateMenu("Instellingen")
$installitem    = TrayCreateItem("Installeren", $settingsitem)
$autostartitem = TrayCreateItem("AutoStart", $settingsitem)
TrayCreateItem("", $settingsitem)
$initem = TrayCreateItem("Gebruik INI", $settingsitem)
TrayCreateItem("", $settingsitem)

$PingItem = TrayCreateItem("Ping Instellingen", $settingsitem)
TrayCreateItem("")
$aboutitem  = TrayCreateItem("Over")
TrayCreateItem("")
;$aboutitem = TrayCreateItem("About")
;TrayCreateItem("")
$exititem   = TrayCreateItem("Exit")
;; End TrayItems

$Autorun=RegRead("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run","Zuilen")
; MsgBox (64,"Autorun :", $Autorun ) 
If $Autorun = "" Then 
TrayItemSetState($autostartitem,68)
Else
TrayItemSetState($autostartitem,65)
Endif


_SetState()
$setlokatie = _ReadIni()
;MsgBox(0,"Ini", "Value :"& $setlokatie)

$copyright = Chr("169")

If $setlokatie = "0" then 
    
DIM $IP = @IPAddress1; get local IP
$lokatie = StringReplace(StringReplace($IP, "." , "" ),"192168",""); From the ip adress replace the "." with nothing then replace 192168 with nothing.
; MsgBox (64,"ip = ", $lokatie )
Select
    case $lokatie = "17343"; If ip adress ends with
        $lokatie = "EINDHOVEN";  Lokatie = City
    case $lokatie = "17415"
        $lokatie = "EINDHOVEN"
    case $lokatie = "166"
        $lokatie = "HEERLEN"
    case $lokatie = "17715"
        $lokatie = "HEERLEN"
    case $lokatie = "17845"
        $lokatie = "HEERLEN"
    case $lokatie = "18215"
        $lokatie = "HELMOND"
    case $lokatie = "17015"
        $lokatie = "MAASTRICHT"
    case $lokatie = "17853"
        $lokatie = "MAASTRICHT"
    case $lokatie = "17615"
        $lokatie = "NIJMEGEN"
    case $lokatie = "17815"
        $lokatie = "NIJMEGEN"
    case $lokatie = "17834"
        $lokatie = "NIJMEGEN"
    case $lokatie = "17867"
        $lokatie = "ROERMOND"
    case $lokatie = "16915"
        $lokatie = "SITTARD"
    case $lokatie = "17860"
        $lokatie = "VENLO"
    case $lokatie = "17915"
        $lokatie = "VENLO"
    case Else
        $lokatie = _SelectLok()

    EndSelect
Else
    If StringRight( @scriptname,3) = "au3" Then
        $inifile = StringReplace(@Scriptname,".au3",".ini")
        Else
        $inifile = StringReplace(@Scriptname,".exe",".ini")
    Endif
        If not FileExists($inifile) Then
            MsgBox (16 , "Fout :","Het bestand " & $inifile & " is niet gevonden" & @CRLF & "Er wordt een bestand aangemaakt")
            IniWrite($inifile,"Instellingen","Lokatie","Geen")
        Else
            $lokatie = IniRead( $inifile,"Instellingen","Lokatie","Geen")
            If $lokatie = "Geen" Then
                MsgBox (16 , "Fout :","Het bestand " & $inifile & " bevat een fout op de lokatie sleutel : " & $lokatie & @CRLF & "Herstel de fout en start het programma opnieuw")
                terminate()
            Else
            MsgBox (64,"Lokatie :", "Lokatie ingesteld op : " & $lokatie ,2)
            EndIf
        Endif
EndIf


$Form1 = GUICreate("Zuilen Pagina", 1024, 772, (@DesktopWidth - 1024) /2, (@DesktopHeight - 768) /2,  BitOR($WS_POPUP,$WS_EX_TOPMOST  )); create the GUI object
; The Internet Explorer object will be projected on this object.

GUISetOnEvent($GUI_EVENT_CLOSE, "terminate")

$Obj = _IECreateEmbedded ( )
$browser = GUICtrlCreateObj($Obj, 0, 0, 1024, 772 )
GUISetState(@SW_SHOW)
_IENavigate ($Obj, 'about:blank')
dim $sHTML
$sHTML = "<HTML><BODY><BR><BR><BR><CENTER><h1 style='font-family : ARIAL; color: #000377'>" _
        & "Er wordt verbinding gezocht met de server ...</h1><BR>" _
        & " <h2 style='font-family : ARIAL; font-size : 20px; font-weight :bold; color: #000377'>Een moment geduld a.u.b.</h2>" _
        & "<BR><BR><BR><h2 style='font-family : ARIAL; color: #000377'> Lokatie : " & $lokatie & "</h2></CENTER></body></html>"
_IEBodyWriteHTML ($Obj, $sHTML)

$online = 1
$wasonline = 0

sleep ( 2000 )
dim $pingwaarde
$ping = RegRead("HKEY_CURRENT_USER\Software\Zuilen","PingSpeed")

while   1
        if Ping("www.Google.nl",$ping ) Then
    ;   MsgBox(0,"Ping","MS : " & $ping )
        $online = 1
    Else
        $online = 0
    EndIf
    if $wasonline <> $online Then
        WinActivate($Form1)
        $wasonline = $online
        if $online Then
            _IENavigate ($Obj, 'http://www.Google.nl')

        Else
            _IENavigate ($Obj, 'about:blank')
            $sHTML = "<HTML><BODY><CENTER><BR><BR><BR><h1 style='font-family : ARIAL; font-size : 24px; font-weight" _
                    & ":bold; color: #000377'>De server is tijdelijk niet bereikbaar. </h1><BR><BR><h2 style='font-family : ARIAL;" _
                    & "font-size : 20px; font-weight :bold; color: #000377'>De verbinding wordt automatisch hersteld ...</h2>"
            _IEBodyWriteHTML ($Obj, $sHTML)
        EndIf
        Sleep (2000);;;;- If Here it never seems to sleep
    EndIf


_SetState()                ;;;;-  Is this the best place for this to be ?
HotKeySet ("!{F4}","terminate");;;;- And what about this ?

    $msg = TrayGetMsg()
            Select
            case $msg = $GUI_EVENT_CLOSE
                terminate()
            case $msg = $installitem
                  _Install()
              case $msg = $autostartitem
                  autostart()
              case $msg = $initem
                  _SetIni()
              case $msg = $PingItem
                  $pingwaarde = _SetPingValue()
                  $ping = $pingwaarde
                ; MsgBox(0,"", $ping & " - " & $pingwaarde )
               Case $msg = $aboutitem
                Msgbox(64, "Over:", "Google Zuilen Emulator" & @CRLF & "Ruud Sas "  & " (2006)"& @CRLF & "Version : 1.3b" )
            Case $msg = $exititem
                Exit
                terminate()
            Case Else
            ;;
     EndSelect
Sleep (2000);;;;- If Here  systemtray does not respond
Wend

Share this post


Link to post
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
Sign in to follow this  
Followers 0