Jump to content

InetRead Prevent Hang Script?


Blueman
 Share

Recommended Posts

Hi all,

I was wondering if you can help me with the function called; InetRead().
My scripts are using this function a lot for several conditions and everything works fine!

But sometimes when the server is a little bit buggy of simply not available my script is hanging.
It takes about 90sec before this function returns a Timeout, when i adjust the parameter it still is hanging about 90sec.

The following script is a example where the script is hanging for aprox. 90sec;

; Set Timeout to 2sec
AutoItSetOption ("TCPTimeout", 2000)

; Read Website
InetRead("http://www.geenverbinding.nl/",1)

; Show Msgbox before Ending Script.
Msgbox(64,"","Finished")

 

The following script is a example where the script show the Msgbox pretty fast;

 

; Set Timeout to 2sec
AutoItSetOption ("TCPTimeout", 2000)

; Read Website
InetRead("http://www.google.nl/",1)

; Show Msgbox before Ending Script.
Msgbox(64,"","Finished")

 

My question now is, what am i doing wrong and/or is there a other way to prevent Hanging the script?

Thanks all :)

 

Link to comment
Share on other sites

Hey Guys,

Searching multiple forum Topic's i realized that a timeout option isn't coming soon on this function.
So i created a simple alternative for the InetRead Function.

My function is first looking for the domain prefix by using the _URLSplit() Function from SmOke_N (Thanks! :) )
Then the function simply tries a InetGet on this domain withhout waiting for it, and starting a loop for Timeout.

If InetGetInfo is receiving a Download Complete command the function is relaying the complete URL to the original InetRead() function, then returning the Data.

For yours to use if needed, it is working perfectly here :)

#include <InetConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <array.au3>


; Read Website
_InetRead("http://www.geenverbinding.nl/",1,2000)

; Show Msgbox before Ending Script.
Msgbox(64,"","Finished")


Func _InetRead($IR_URL,$IR_OPTIONS = 1,$IR_TIMEOUT = 2000)

    ; Define Var's
    Local $sFilePath = _WinAPI_GetTempFileName(@TempDir)
    Local $ProcessCounter = 0
    Local $ReadedOutput, $GetDomain

    $GetDomain = _URLSplit($IR_URL)

    If NOT IsArray($GetDomain) Then Return ""

    ; Start Connection to Server, Forced Reload.
    Local $hDownload = InetGet($GetDomain[1]&"://"&$GetDomain[2], $sFilePath, 1, 1)

    ; Start loop for Timeout
    Do
        Sleep(50)
        $ProcessCounter += 1
    Until InetGetInfo($hDownload, 2) OR $ProcessCounter > ($IR_TIMEOUT/50)

    ; Close the handle returned by InetGet.
    InetClose($hDownload)

    ; Delete the file.
    FileDelete($sFilePath)


    If $ProcessCounter > ($IR_TIMEOUT/50) Then Return ""


    $ReadedOutput = InetRead($IR_URL,$IR_OPTIONS)
    Return $ReadedOutput

EndFunc

Func _URLSplit($szUrl)
    Local $sSREPattern = '^(?s)(?i)(http|ftp|https|file)://(.*?/|.*$)(.*/){0,}(.*)$'
    Local $aUrlSRE = StringRegExp($szUrl, $sSREPattern, 2)
    If Not IsArray($aUrlSRE) Or UBound($aUrlSRE) - 1 <> 4 Then Return SetError(1, 0, 0)
    If StringRight($aUrlSRE[2], 1) = '/' Then
        $aUrlSRE[2] = StringTrimRight($aUrlSRE[2], 1)
        $aUrlSRE[3] = '/' & $aUrlSRE[3]
    EndIf
    $szProtocol = $aUrlSRE[1]
    $szDomain = $aUrlSRE[2]
    $szPath = $aUrlSRE[3]
    $szFile = $aUrlSRE[4]
    Return $aUrlSRE
EndFunc   ;==>_URLSplit

 

Just paste these Functions in your script, then simply add the Underscore to all your InetRead functions, enjoy! :)

PS: Standard Timeout Setting is 2000 (Milliseconds), but you can change it to your needs.

Edited by Blueman
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

×
×
  • Create New...