Subject |
Re: How to Catch In Use By another error? |
From |
Mervyn Bick <invalid@invalid.invalid> |
Date |
Mon, 20 Nov 2017 10:45:54 +0200 |
Newsgroups |
dbase.getting-started |
On 2017-11-20 5:15 AM, Michael wrote:
> Hi Guys,
>
> I need some help. I am polling a folder waiting for a file to be written to it, so im sitting in a do while loop using if f.exists(filename) then proceed.
>
> The problem I have is sometimes the f.exists picks up the file is being written so I get a true status and not complete yet or closed and I get a Ïn Use By Another"error message.
>
> Ive tried to use a try and catch exception and finally to continue but im still getting the error.
>
> The question is how do I or what could I use to see if the file exists but is closed so that i do not get an error?
>
> Im using windows 7 or windows 10 and Dbase 11.00
>
> Any help is appreciated.
>
> Regards,
>
> Michael.
>
The exists() method of the file returns true the moment the file is
created. It does not, however, know if the file is in use or not.
What you need to do is wrap the file's open() method in a loop in the
try..catch...entry construct which will keep on trying to open the file
until it succeeds. It would be a good idea to include a reasonable time
limit "just in case". 5 seconds can seem like "forever" while waiting
for a program to time out so you might consider adding code to indicate
the number of tries to the user especially if you find you need to wait
more than 5 seconds.
The following is untested and it's "one cup of coffee" code (CYA
statement :-) ) but it should give you a starting point.
nnSecs = 5 //seconds before timeout
nStart = seconds()
lFileAvailable = false
f = new file()
do while lFileAvailable <> true or seconds()-nStart <= nSecs
try
lFileAvailable = f.open('whatever')
if lFileAvailable = true
exit //exit from loop if file becomes available in the time set
endif
catch(exception e)
sleep(nSecs/10) //allows 10 tries in set time
if seconds() - nStart > nSeconds
exit //exit from loop if file is not available within time set
endif
endtry
enddo
if not lFileAvailable
msgbox('Timed out')
//code to deal with this
endif
// rest of program
Mervyn.
|
|