Subject Re: How to Catch In Use By another error?
From Mervyn Bick <invalid@invalid.invalid>
Date Mon, 20 Nov 2017 17:51:02 +0200
Newsgroups dbase.getting-started
Attachment(s) test_inuse.prg

On 2017-11-20 1:35 PM, Michael wrote:
> 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.

f.open() will only return true if it actually opens the file.  If the
file can't be opened f.open() will return false but it will also trigger
the error response.   By using a try...catch...endtry construct dBASE
will "eat" the error response but retain the false value returned in a
memvar or user defined property.

The delay in my suggested code has no effect on the code.  It's there
because I feel it is pointless to keep trying continually.  On my
computer without the sleep() instruction dBASE retries just under 2000
times a second. By all means remove the sleep() instruction.  At best
though it will only cut your waiting time by half a second.

The code I posted this morning was untested.  I've now created a little
test program.  Run it as it stands and it times out without having
opened the text file.  Uncomment the marked code and this will close the
text file after 2 seconds.  Once this is done the text file can be
opened as used as required.

Mervyn.








clear
nSecs = 4
f1 = new file()
f1.create('dummy.txt')
f1.puts('test text')
nStart = seconds()
nCount = 0
lFileAvailable = false
f = new file()
do while lFileAvailable <> true or seconds()-nStart <= nSecs
   ? ++ nCount,seconds() - nStart
   try
      lFileAvailable = f.open('dummy.txt')
      if lFileAvailable = true
         exit  
      endif  
   catch(exception e)
      sleep(nSecs/10)
      
//   Uncomment the code below.  This will close dummy.txt
//   after 2 seconds which will allow the progrm to continue.
/*      
      if seconds() -nStart > 2
         f1.close()
      endif  
*/
      if seconds() - nStart > nSecs
         exit
      endif  
   endtry
enddo
? 'done' , seconds() - nStart,lFileAvailable
if not lFileAvailable
   msgbox('Timed out')
   //code to deal with this
endif
try
   cRead = f.gets()
   ?cRead
catch(exception e)
endtry
f1.close()
f.close()