Jump to content
BatMan22

FileOpen error due to network lag?

Recommended Posts

BatMan22

So when I run the code below.. the first time after a long amount of time (unknown but roughly 5minutes+ )... The script returns a -1 value. I suspect that it's due to network lag.. because if I run the function again.. it works fine. There are no stored variables or any issues like that.. so what I am wondering is if there is any way to 'prime' the network to get it ready for traffic or if I can get FileOpen to wait longer before returning with the -1 value. The directory always exists, so it isn't a directory error and the file existing doesn't seem to make any difference if it errors out or not. It just seems like maybe because of network lag FileOpen returns an error if it doesn't get a response quick enough.. Ideas?

 

$filetocleanup = "X:\MacroShare\IC3.txt"
    Local $sS = FileOpen($filetocleanup, 1)
    If $sS = -1 Then
;~      _DebugError(@error)
        MsgBox("ERROR", "ERROR @ LINE # " & @ScriptLineNumber, 10)
        ConsoleWrite("Error @ " & @ScriptLineNumber & @error & @CRLF)
        Exit
    EndIf

 

Share this post


Link to post
Share on other sites
orbs
Posted (edited)

there is probably a better solution depending on the exact details of your environment, but here's a quick and dirty one - replace your FileOpen() call with this _FileOpenRetry():

Func _FileOpenRetry($sFile, $iFlag = 0, $iTimeout = 0)
    Local $hFile = -1, $iError = 0, $iExtended = 0
    Local $hTimer = TimerInit()
    Do
        $hFile = FileOpen($sFile, $iFlag)
        $iError = @error
        $iExtended = @extended
    Until $hFile <> -1 Or TimerDiff($hTimer) > $iTimeout
    Return SetError($iError, $iExtended, $hFile)
EndFunc   ;==>_FileOpenRetry

 

Edited by orbs
  • Like 1

Share this post


Link to post
Share on other sites
jdelaney
Posted (edited)

Add in file logging to know for certain where your script is failing.  Also, make sure the file exists first...just because the directory is always there for you, doesn't mean (and most definitely will not be) present when running as a different user nor with elevated permissions (running as admin).

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
jchd

Could the remote device enter a sleep state or power down mode after some time of inactivity? That would explain why subsequent accesses succeed quickly.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
BatMan22
48 minutes ago, orbs said:

there is probably a better solution depending on the exact details of your environment, but here's a quick and dirty one - replace your FileOpen() call with this _FileOpenRetry():

Func _FileOpenRetry($sFile, $iFlag = 0, $iTimeout = 0)
    Local $hFile = -1, $iError = 0, $iExtended = 0
    Local $hTimer = TimerInit()
    Do
        $hFile = FileOpen($sFile, $iFlag)
        $iError = @error
        $iExtended = @extended
    Until $hFile <> -1 Or TimerDiff($hTimer) > $iTimeout
    Return SetError($iError, $iExtended, $hFile)
EndFunc   ;==>_FileOpenRetry

 

I will give this a shot.. Thanks :)

Share this post


Link to post
Share on other sites
BatMan22
46 minutes ago, jdelaney said:

Add in file logging to know for certain where your script is failing.  Also, make sure the file exists first...just because the directory is always there for you, doesn't mean (and most definitely will not be) present when running as a different user nor with elevated permissions (running as admin).

How do I enable file logging? Also the file existing/not existing doesn't matter in write mode.. ie append mode in this case.. right? Either way, I have tried deleting the file and not-deleting the file before running the command and there's no difference. 

19 minutes ago, jchd said:

Could the remote device enter a sleep state or power down mode after some time of inactivity? That would explain why subsequent accesses succeed quickly.

The machine isn't in a real sleep mode ever but the hard drive could perhaps go to sleep and maybe that is what is causing the increased access time enough to error out?

Share this post


Link to post
Share on other sites
BatMan22

So I'm still stuck.. apparently it's not network lag.. because even with 30+ second timers... sometimes FileOpen just still keeps returning -1 until I re-run it. Is there anyway to find out a more detailed error then just -1 for FileOpen?

Share this post


Link to post
Share on other sites
jdelaney

you create a file with _filecreate, and you log actions to that new file with FileWriteLine.


IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
BatMan22
2 minutes ago, jdelaney said:

you create a file with _filecreate, and you log actions to that new file with FileWriteLine.

Oh, lol. I just use consolewrite.. Also I *think* I semi-solved the issue without ever figuring out what was wrong.. By replacing the file delete that runs earlier in the program with a fileopen with -2 to just clear it then fileclose.. it seems to have solved the issue.. don't know why. I was under the impression that FileOpen($filetocleaup, 1) would create the file if it didn't exist.. but if that was the issue.. then running it over and over wouldn't fix the issue right? :) 

Share this post


Link to post
Share on other sites
Earthshine

i use log4a.au3

 


My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
Chimp

Is the host sharing the file in the same LAN or in a remote WAN?
maybe your network can have some "holes"...?
try to open a separate cmd window and issue a continuous ping to the host sharing the target file
ping -t xxx.xxx.xxx.xxx
where xxx.xxx.xxx.xxx is the ip number or the hostname of the host sharing the file, in this way you can see if the "duration" parameter is stable or it is floating a lot or, even worst, if you have some "holes" in the ping responses...


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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

×