Subject Re: How to Catch In Use By another error?
From Michael <michael@abstractservices.com.au>
Date Mon, 20 Nov 2017 06:35:36 -0500
Newsgroups dbase.getting-started

Thanks for that Mervyn, yeah, actually my code is very similar, almost identical but I found if the file was in use, it triggered an error and the catch wouldnt work because the program halted an in use error.

if the f.open returns true, wouldnt it have the same effect? If it opens the file it will only trigger a "file already in use" error if i try to write to it, it doesnt mean its its not still locked and being written to. It will only open it in read mode.

The file im opening is only a txt file. could I use set reprocess to -1 to keep trying until its unlocked?

or is that different again?

The delay idea is probably the best way, if there is no other way to check if in use.

Mervyn Bick Wrote:

> 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.
>
>
>
>
>
>
>
>
>
>