From: fv@tcenl.nospam.com   
      
   In the obscure news:1128668078.984102.77090@o13g2000cwo.googlegroups.com,   
    Someone calling himself Wim   
    suspiciously hiding as uttered:   
   > A TP program crashes sometimes because the file it tries to   
   > rewrite/rename is locked by a virusscanner. I would like to include   
   > some code to check for a locked file and wait some time to see if the   
   > file lock has been removed.   
   >   
   > Typically a function like the following will be invoked just before   
   > doing a (major) file operation:   
   >   
   > function WaiFF( const FileName: string ): Boolean ;   
   > {*   
   > * Function Wait_File_Free returns a true value if file FileName is   
   > not   
   > * locked. If the file is locked, it will wait for some time. WaiFF   
   > will   
   > * return a false value if the file is still locked.   
   > *}   
   > const   
   > MaxTries= 3 ; { Maximum number of tries minus one }   
   > var   
   > WaitTime: Integer ; { Time lapse }   
   > i : Integer ; { Loop control variable }   
   > begin   
   > WaiFF:= True ; { Expected function result }   
   >   
   > if ufFileLocked( FileName ) then   
   > begin   
   > WaitTime:= 1 ; { Waiting time in seconds }   
   > for i:=0 to MaxTries do   
   > begin   
   > ufTimedWait( WaitTime ) ;   
   > WaitTime:= WaitTime * 2 ; { Increase wait time for next pass }   
   > if not ufFileLocked( FileName ) then Exit ;   
   > end ; { of for}   
   > end   
   > else { if file not locked then }   
   > Exit ;   
   >   
   > WriteLn( LogFile, 'Error - File ', FileName, ' is locked' ) ;   
   > WaiFF:= False ;   
   > end ;   
   >   
   > Searching the web i did not find any code snippets for implementing   
   > the functions ufFileLocked and ufTimedWait. For the latter function,   
   > the function Wait in TPL is probably not a good choice ,as it is only   
   > a CPU-time-consuming loop. The process which locked the file (in my   
   > case a virusscanner), should get the CPU to finish it's job.   
   >   
   > Can someone give me pointers on how to write those two uf* functions?   
   >   
      
   Why so difficult?   
      
   The standard way of preventing an IO crash in TP is by switching off   
   input/output checking   
   code {$I-}   
   In order to check the result call the IOResult function.   
   Look for both of these in the TP help file. {shift F1}   
   e.g.   
      
    var F: text;   
    begin   
    Assign(F, 'c:\autoexec.bat');   
    {$I-} {turn off IO error checking, }   
    Reset(F);   
    if IOResult <> 0 then   
    Writeln('File opening error');   
    else   
    begin   
    while not eof(f) do   
    ........ {start reading}   
    close(F);   
    end;   
    {$I+} {turn IO checking back on}   
    end.   
      
      
      
      
   --   
   Femme Verbeek   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|