InetRead TimeOut? Script stürtzt ab!

  • Guten Morgen :)

    Ich arbeite gerade an einem Script welches die Verfügbarkeit einer URL testen soll. Nun solange die URL verfügbar ist läuft alles einwandfrei. Ist die Seite zwar im DNS auflösbar aber nicht erreichbar, schmiert das komplette Script ab.

    Ich verwende AdLib für die wiederkehrende Aktion.

    Mein Script ist wie folgt aufgebaut:

    [autoit]

    ;Register interval tasksAdlibRegister("_hStatus_SystemTime",1000)AdlibRegister("_CheckForInternetConnection",10000)
    Func _CheckForInternetConnection()$Result=_CheckInternet(_DBSettings_GET("ConnectionCheckURL","http://update.salvationarmy.ch/sysman/connectiontest.html"),_DBSettings_GET("ConnectionCheckValue","142199AB5289B0EA68E697DD4C0E6E7081ECBF385501722D60D7233FD984"))if $Result=0 then$InternetConnection=0_GUICtrlStatusBar_SetText($hStatus,"Internet connection unavailable! ={",0)ConsoleWrite("No Internet :(")Else$InternetConnection=1_GUICtrlStatusBar_SetText($hStatus,"Internet connection available! =}",0)ConsoleWrite("Working :)")EndIfEndFunc
    Func _hStatus_SystemTime()$SystemTime=_Date_Time_GetSystemTime()_GUICtrlStatusBar_SetText($hStatus,_Date_Time_SystemTimeToDateStr($SystemTime) & " - " & _Date_Time_SystemTimeToTimeStr($SystemTime),2)EndFunc
    Func _CheckInternet($strURL, $strCheckValue)$Result=BinaryToString(InetRead($strURL),16)if $Result=$strCheckValue Thenreturn 1Elsereturn 0EndIfEndFunc

    [/autoit]


    Gibt es eine Möglichkeit wie eine Art TimeOut einzubauen? Sprich wenn die Seite nicht inerhalb von z.B. 1 Sekunde geladen wird, fehler ausgeben?

    Besten Dank für eure Unterstützung.

    Gruss
    Iwasready

  • Natürlich :) Sorry wenn die Formatierung nicht gepasst hat, habe vergesen auf "Quellcode" umzuschalten.
    Script 1: Hauptscript

    Spoiler anzeigen
    [autoit]

    #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
    #AutoIt3Wrapper_icon=Ressources\icoSysMan.ico
    #AutoIt3Wrapper_outfile=SysMan_Client.exe
    #AutoIt3Wrapper_UseUpx=n
    #AutoIt3Wrapper_Res_Comment=System Manager based on AutoIt 3.x
    #AutoIt3Wrapper_Res_Fileversion=1.0.0.0
    #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
    ;System Maintenance Client
    ;*************************
    ; Developed for the Salvation Army Switzerland

    [/autoit] [autoit][/autoit] [autoit]

    ;Includes
    #include "UDF_SysMan.au3"
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include 'Ribbonsbar.au3'
    #Include
    #include
    #include

    [/autoit] [autoit][/autoit] [autoit]

    ;Global variables
    Global $InternetConnection=0 ;0 = no Internet, 1 = Internet connection is available
    Global $SQLiteDB;SQLite db handle
    Global $ApplVersion = "1.0.0";Version of Application (used for identification)

    [/autoit] [autoit][/autoit] [autoit]

    ;------------------------------------------------------------------------------------------------------ START STARTUP SCRIPTS
    if FileExists(@ScriptDir & "\Startup") Then
    $StartupScriptFiles = _FileListToArray(@ScriptDir & "\Startup","*.au3")
    $i=0
    while $i<$StartupScriptFiles[0]
    $i=$i+1
    RunWait(@AutoItExe & " " & @ScriptDir & "\Startup\" & $StartupScriptFiles[$i],"",@SW_HIDE)
    WEnd
    $i=0
    EndIf
    ;------------------------------------------------------------------------------------------------------ END STARTUP SCRIPTS

    [/autoit] [autoit][/autoit] [autoit]

    ;------------------------------------------------------------------------------------------------------ START DATABASE
    _SQLite_Startup ()
    If @error Then
    MsgBox(16, "SQLite Error", "SQLite.dll Can't be Loaded!")
    Exit - 1
    EndIf
    ConsoleWrite("_SQLite_LibVersion=" &_SQLite_LibVersion() & @CRLF)
    $SQLiteDB = _SQLite_Open(IniRead("sysmandata.ini","SysMan","Database","sysmandata.sqlite"))
    If @error Then
    MsgBox(16, "SQLite Error", "Can't Load Database!")
    Exit - 1
    EndIf
    ;------------------------------------------------------------------------------------------------------ END DATABASE
    ;############################################################################
    _Log("SysMan","Application started!")
    ;Check Running Permission (has to be Administrator)
    ;_RunningAsAdmin("","","")

    [/autoit] [autoit][/autoit] [autoit]

    ;------------------------------------------------------------------------------------------------------ START GUI INITIALISATION
    $hGUI = GUICreate ( "System Manager - v." & $ApplVersion , 900, 500, -1, -1)
    Opt("GUIOnEventMode",1)

    [/autoit] [autoit][/autoit] [autoit]

    GUISetOnEvent($GUI_EVENT_CLOSE, "_GuiEvent_CLOSE")
    GUISetOnEvent($GUI_EVENT_MINIMIZE, "_GuiEvent_MINIMIZE")

    [/autoit] [autoit][/autoit] [autoit]

    GUISetBkColor( 0x99B9E3 );Gui BG Blue
    ;~ GUISetBkColor( 0x4C4C4C );Gui BG Black
    ;~ GUISetBkColor( 0xC9CCD5 );Gui BG Silver
    $hBar = _RibbonsBar_Create( $hGUI, 0, 0, 900, 0, 'blue')

    [/autoit] [autoit][/autoit] [autoit]

    ;Create statusbar in GUI
    Local $StatusBarParts[3] = [300, 600, -1]
    $hStatus = _GUICtrlStatusBar_Create ($hGUI,$StatusBarParts)
    _GUICtrlStatusBar_SetText($hStatus,"System Manager started!",1)
    _GUICtrlStatusBar_SetText($hStatus,"Internet connection not yet tested!",0)
    ;------------------------------------------------------------------------------------------------------ END GUI INITIALISATION

    [/autoit] [autoit][/autoit] [autoit]

    ;------------------------------------------------------------------------------------------------------ BEGIN TAB System Manager
    $hTab_SysMan = _RibbonsBar_Create_Tab( $hBar, 'System Manager' )
    $hItem_File = _RibbonsBar_Create_TabItem ( $hTab_SysMan, 'File' , 160 )
    $hButton_File_Close = _RibbonsBar_Create_SmallButton($hItem_File,'',132,1 ,'Close',80)
    $hButton_File_Hide = _RibbonsBar_Create_SmallButton($hItem_File,'',270,2 ,'Hide',80)

    [/autoit] [autoit][/autoit] [autoit]

    ;------------------------------------------------------------------------------------------------------ END TAB System Manager
    _RibbonsBar_Tab_SetState($hTab_SysMan);set active tab!
    GUISetState()
    ;------------------------------------------------------------------------------------------------------ START TRAY ICON
    Opt("TrayAutoPause",0)
    Opt("TrayMenuMode",1)
    $TrayIcon_Close = TrayCreateItem("Close")
    $TrayIcon_Show= TrayCreateItem("Show")
    ;------------------------------------------------------------------------------------------------------ START TRAY ICON

    [/autoit] [autoit][/autoit] [autoit]

    ;Register interval tasks
    AdlibRegister("_hStatus_SystemTime",1000)
    AdlibRegister("_CheckForInternetConnection",10000)

    [/autoit] [autoit][/autoit] [autoit]

    ;------------------------------------------------------------------------------------------------------ START GUI INPUT
    While 1
    ;Input from RibbonsBar
    Switch _RibbonsBar_GetMsg()
    Case -3
    ;Close
    Exit
    Case $hButton_File_Close
    if msgbox(4,"Close?","Would you realy like to close this application?") = 6 Then
    Exit
    EndIf
    Case $hButton_File_Hide
    GuiSetState(@SW_HIDE,$hGUI)
    EndSwitch

    [/autoit] [autoit][/autoit] [autoit]

    ;Input from GUI
    ;Switch GUIGetMsg()
    ;EndSwitch

    [/autoit] [autoit][/autoit] [autoit]

    ;Input from TryIcon
    Switch TrayGetMsg()
    case $TrayIcon_Show
    GUISetState(@SW_SHOW,$hGUI)
    case $TrayIcon_Close
    Exit
    EndSwitch

    [/autoit] [autoit][/autoit] [autoit]

    WEnd
    ;------------------------------------------------------------------------------------------------------ END GUI INPUT

    [/autoit] [autoit][/autoit] [autoit]

    ;------------------------------------------------------------------------------------------------------ START INTERVAL FUNCTIONS

    [/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _hStatus_SystemTime()
    ; Description ...: Display the current system time inside the application
    ; Syntax.........: _hStatus_SystemTime()
    ; Author ........: Y. Menge ([email='yves_menge@swi.salvationarmy.org'][/email])
    ; ===============================================================================================================================
    Func _hStatus_SystemTime()
    $SystemTime=_Date_Time_GetSystemTime()
    _GUICtrlStatusBar_SetText($hStatus,_Date_Time_SystemTimeToDateStr($SystemTime) & " - " & _Date_Time_SystemTimeToTimeStr($SystemTime),2)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _CheckForInternetConnection()
    ; Description ...: Check if the needed host is available on the network
    ; Syntax.........: _CheckForInternetConnection()
    ; Author ........: Y. Menge ([email='yves_menge@swi.salvationarmy.org'][/email])
    ; ===============================================================================================================================
    Func _CheckForInternetConnection()
    $Result=_CheckInternet(_DBSettings_GET("ConnectionCheckURL","http://update.salvationarmy.ch/sysman/connectiontest.html"),_DBSettings_GET("ConnectionCheckValue","142199AB5289B0EA68E697DD4C0E6E7081ECBF385501722D60D7233FD984"))
    if $Result=0 then
    $InternetConnection=0
    _GUICtrlStatusBar_SetText($hStatus,"Internet connection unavailable! ={",0)
    ConsoleWrite("No Internet :(")
    Else
    $InternetConnection=1
    _GUICtrlStatusBar_SetText($hStatus,"Internet connection available! =}",0)
    ConsoleWrite("Working :)")
    EndIf
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;------------------------------------------------------------------------------------------------------ END INTERVAL FUNCTIONS

    [/autoit] [autoit][/autoit] [autoit]

    ;------------------------------------------------------------------------------------------------------ START INTERNAL FUNCTIONS

    [/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _GuiEvent_MINIMIZE
    ; Description ...: Hide the GUI!
    ; Syntax.........: _GuiEvent_CLOSE()
    ; Author ........: Y. Menge ([email='yves_menge@swi.salvationarmy.org'][/email])
    ; ===============================================================================================================================
    Func _GuiEvent_MINIMIZE()
    GUISetState(@SW_HIDE)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _GuiEvent_CLOSE
    ; Description ...: Close open databases and exit the application
    ; Syntax.........: _GuiEvent_CLOSE()
    ; Author ........: Y. Menge ([email='yves_menge@swi.salvationarmy.org'][/email])
    ; ===============================================================================================================================
    Func _GuiEvent_CLOSE()
    ;Close database
    _SQLite_Close()
    _SQLite_Shutdown ()

    [/autoit] [autoit][/autoit] [autoit]

    _Log("SysMan","Application cloesd!")

    [/autoit] [autoit][/autoit] [autoit]

    ;exit application
    Exit
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;------------------------------------------------------------------------------------------------------ END INTERVAL FUNCTIONS

    [/autoit]

    Script 2:

    Spoiler anzeigen
    [autoit]


    ;Include needed Files
    #include

    [/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _Log
    ; Description ...: Creat a log entry in the database
    ; Syntax.........: _FileListToArrayEx($sPath, $sFilter = '*.*', $iFlag = 0, $sExclude = '')
    ; Parameters ....: $strType - Type of log entry
    ; $sArcName - Value of log entry
    ; Return values .: Success - Returns 1
    ; Failure - Returns 0
    ; Author ........: Y. Menge ([email='yves_menge@swi.salvationarmy.org'][/email])
    ; ===============================================================================================================================
    Func _Log($strType, $strValue)
    _SQLite_Exec(-1,"INSERT INTO Log (""Type"",""Value"") VALUES("""& $strType &""","""& $strValue & """)")
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _ArraySize
    ; Description ...: Get the lenght of a array! Works with empty arrays too (returns 0)
    ; Syntax.........: _ArraySize($aArray)
    ; Parameters ....: $aArray - Array name
    ; Return values .: Success - Lenght of the given array
    ; Author ........: Y. Menge ([email='yves_menge@swi.salvationarmy.org'][/email])
    ; ===============================================================================================================================
    Func _ArraySize( $aArray )
    SetError( 0 )
    $index = 0
    Do
    $pop = _ArrayPop( $aArray )
    $index = $index + 1
    Until @error = 1
    Return $index - 1
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _RunningAsAdmin()
    ; Description ...: Check admin priviledges. If scripts is running without admin priviledges, restart the script as admin!
    ; Syntax.........: _RunningAsAdmin($strAdminUser,$strAdminPassword,$strAdminDomain)
    ; Parameters ....: $strAdminUser- Admin Account
    ;$strAdminPassword- Account Password
    ;$strAdminDomain- Account Domain to log on
    ; Author ........: Y. Menge ([email='yves_menge@swi.salvationarmy.org'][/email])
    ; ===============================================================================================================================
    Func _RunningAsAdmin($strAdminUser, $strAdminPassword, $strAdminDomain)
    If IsAdmin() = 0 Then
    if RunAs($strAdminUser,$strAdminDomain,$strAdminPassword,0,FileGetShortName(@ScriptFullPath)) = 0 Then
    _Log("SysMan","Could not start as Admin! AminUser, AdminPassword and LogonDomain correct?")
    Exit
    EndIf
    Exit
    EndIf
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _DBSettings_GET()
    ; Description ...: Get a setting out of database
    ; Syntax.........: _DBSettings_GET($strName, $strDefaultValue)
    ; Parameters ....: $strName- Settingname to get
    ;$strDefaultValue- Defaultvalue if setting ist not in database
    ; Author ........: Y. Menge ([email='yves_menge@swi.salvationarmy.org'][/email])
    ; ===============================================================================================================================
    Func _DBSettings_GET($strName, $strDefaultValue)
    local $aResult, $hQuery
    _SQLite_Query(-1,"SELECT value FROM settings WHERE name='" & $strName & "'",$hQuery)
    _SQLite_FetchData($hQuery,$aResult)
    _SQLite_QueryFinalize($hQuery)
    ;return default value if none is saved inside db
    if $aResult[0]="" Then
    return $strDefaultValue
    EndIf
    ;return db value
    return $aResult[0]
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _DBSettings_SET()
    ; Description ...: Set or modifie a setting to a new value
    ; Syntax.........: _DBSettings_SET($strName, $strValue)
    ; Parameters ....: $strName- Settingname
    ;$strValue- Value to set
    ; Author ........: Y. Menge ([email='yves_menge@swi.salvationarmy.org'][/email])
    ; ===============================================================================================================================
    Func _DBSettings_SET($strName, $strValue)
    local $aResult, $hQuery
    ;Check if value allready exists!
    _SQLite_Query(-1,"SELECT name FROM settings WHERE name='" & $strName & "'",$hQuery)
    _SQLite_FetchData($hQuery,$aResult)
    _SQLite_QueryFinalize($hQuery)
    if $aResult[0]="" Then
    ;Create a new setting in db
    _SQLite_Exec(-1,"INSERT INTO settings (name,value) VALUES ('" & $strName & "','" & $strValue & "')")
    Else
    ;Modify a existing setting in db
    _SQLite_Exec(-1,"UPDATE settings SET value='" & $strValue & "' WHERE name='" & $strName & "'")
    EndIf
    _SQLite_QueryFinalize($hQuery)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _DBSettings_DEL()
    ; Description ...: Remove a setting from the table
    ; Syntax.........: _DBSettings_DEL($strName)
    ; Parameters ....: $strName- Settingname
    ; Author ........: Y. Menge ([email='yves_menge@swi.salvationarmy.org'][/email])
    ; ===============================================================================================================================
    Func _DBSettings_DEL($strName)
    _SQLite_Exec(-1,"DELETE FROM settings WHERE name='" & $strName & "'")
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _CheckPing()
    ; Description ...: Check the availability of a internet connection to a specific host!
    ; Syntax.........: _CheckPing($strHost)
    ; Parameters ....: $strHost- Host to check
    ; Return values .: Success - Returns >0
    ; Failure - Returns 0
    ; Author ........: Y. Menge ([email='yves_menge@swi.salvationarmy.org'][/email])
    ; ===============================================================================================================================
    Func _CheckPing($strHost)
    $Result = Ping($strHost)
    if $Result > 0 Then
    return $Result
    Else
    return 0
    EndIf
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _CheckInternet()
    ; Description ...: Check the availability of a internet connection to a specific host!
    ; Syntax.........: _CheckInternet($strURL, $strCheckValue)
    ; Parameters ....: $strURL- URL to read in!
    ;$strCheckValue- A string to compare with
    ; Return values .: Success - Returns 1
    ; Failure - Returns 0
    ; Author ........: Y. Menge ([email='yves_menge@swi.salvationarmy.org'][/email])
    ; ===============================================================================================================================
    Func _CheckInternet($strURL, $strCheckValue)
    $Result=BinaryToString(InetRead($strURL),16)
    if $Result=$strCheckValue Then
    return 1
    Else
    return 0
    EndIf
    EndFunc

    [/autoit]


    //Edit: Für die GUI Ribbonbar Oberfläche verwende ich die folgende UDF --> RibbonsBar (Office 2007 Bar) UDF


    Gruss
    Iwasready

    Einmal editiert, zuletzt von iwasready (15. März 2010 um 09:54)

  • Hallo Iwasready,

    jetzt lässt es sich bei mir starten (nach verneinen 2 maligem Nachfragen ob ich es wirklich beenden will) danach bekomme ich aber Fehlermeldungen

    das heisst die DB existiert bei mir nicht, eine Funktion zum Anlegen der benötigten DB kann ich im Skript nicht finden. Evtl. fehlt auch noch sysmandata.ini,

    mfg (Auto)Bert

  • Hallo Iwasready,

    das Skript läuft bei mir ohne abzuschmieren, ist allerdings bedingt durch meinen INet-Zugang (Web'n'walk ca. 12 KB/s) sehr träge (Verzögerungen bis zu 20 sec).
    Die einzige Änderung die ich vorschlagen würde:

    [autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _CheckForInternetConnection()
    ; Description ...: Check if the needed host is available on the network
    ; Syntax.........: _CheckForInternetConnection()
    ; Author ........: Y. Menge ([email='yves_menge@swi.salvationarmy.org'][/email])
    ; ===============================================================================================================================
    Func _CheckForInternetConnection()
    AdlibUnRegister("_CheckForInternetConnection")
    $Result=_CheckInternet(_DBSettings_GET("ConnectionCheckURL","http://update.salvationarmy.ch/sysman/connectiontest.html"),_DBSettings_GET("ConnectionCheckValue","142199AB5289B0EA68E697DD4C0E6E7081ECBF385501722D60D7233FD984"))
    if $Result=0 then
    $InternetConnection=0
    _GUICtrlStatusBar_SetText($hStatus,"Internet connection unavailable! ={",0)
    ConsoleWrite("No Internet :(" & @CRLF)
    Else
    $InternetConnection=1
    _GUICtrlStatusBar_SetText($hStatus,"Internet connection available! =}",0)
    ConsoleWrite("Working :)" & @CRLF)
    EndIf
    AdlibRegister("_CheckForInternetConnection",10000)
    EndFunc

    [/autoit]

    Ich verstehe aber den Sinn der laufenden Überprüfung nicht (vor allem in so kurzen Abständen), es würde doch reichen nur bei Bedarf die Verbindung zu prüfen

    mfg (Auto)Bert

  • Danke für die Rückmeldung!

    Dass die Überprüfung so oft durchgeführt wird ist nur zu Testzwecken.

    Ich werde deine Änderung übernehmen, habe gar nicht soweit gedacht.

    Gruss
    iwasready