Re: determine if file is being used by another process

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Nicholas Paldino [.NET/C# MVP] (mvp_at_spam.guard.caspershouse.com)
Date: 02/15/05


Date: Tue, 15 Feb 2005 17:33:20 -0500

Willy,

    The problem you describe will exist in both cases, and the solution
would be the same in both cases. Basically, the thread should be put to
sleep in between checks. Whether or not the OP uses exceptions to determine
the cause of the error, or the result from CreateFile, the need to put the
thread to sleep in that loop is still needed.

-- 
               - Nicholas Paldino [.NET/C# MVP]
               - mvp@spam.guard.caspershouse.com
"Willy Denoyette [MVP]" <willy.denoyette@pandora.be> wrote in message 
news:erlHT26EFHA.548@TK2MSFTNGP14.phx.gbl...
>
> "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote 
> in message news:eQOYqi6EFHA.2156@TK2MSFTNGP10.phx.gbl...
>> Willy,
>>
>>    The OP is trying to handle business logic depending on whether or not 
>> the file is in use.  When coding business logic, flow control, IMO, 
>> should never be predicated on exception handling, especially when there 
>> is a way to check the status.
>>
>>    Of course, this is a matter of preference.  If other people want to 
>> base their logic on exception handling, they are free to do so, but I 
>> think there is a strong camp that disagrees with this approach.
>>
>>    Also, the OP specifically asked for a solution that did not involve 
>> using exceptions to determine if the file was in use.
>>
>>    From a performance standpoint, I think that the seven or so extra 
>> instructions to call API through the P/Invoke layer (which are going to 
>> be called anyways by the constructor if you didn't pass in a file handle) 
>> are a small price to pay as opposed to throwing an exception.
>>
>>    You are right about the constructor being marked as obsolete and 
>> passing a SafeHandle.  I didn't mention it because I assume that most 
>> people are not using the beta, but the OP should know that as well.
>>
>>
>
> Nicholas,
>
> I'm not talking about the performance differences between both, I'm 
> talking about the extra lines of code that are needed to make it more 
> robust, especially in the case of a windows service.
> And IMO it's pretty wrong to loop on CreateFile calls [without any sleep 
> in between (pun intended) - see later] and only terminate the loop when 
> the status is OK. What if the status is something like "access denied or 
> file does not exist or wrong open mode" you will have to exit the loop and 
> take an action don't you think so?
> Now whiter you handle exceptions or error codes, you should never call an 
> API like CreateFile is a closed loop without some sleep in between the 
> calls.
> If you average file creation time is something like 1 sec., put a sleep of 
> 0.5 - 1 sec. in between the calls, handle the exception or the error 
> return and retry when still busy. That way you will only throw a very few 
> times per file you process. However, if you keep your closed loop your 
> thread will consume most if not all CPU resources and disturb the external 
> process in such a way that it can dramatically increase the file creation 
> time when run on a single CPU box.
>
> Willy.
>
>
> 


Relevant Pages

  • Re: Vista to Unix
    ... It is those exceptions that cause me to look elsewhere. ... I have used Ubuntu on this laptop before, ... displeased about its shoddy wireless networking and ACPI sleep support. ...
    (comp.os.linux.misc)
  • Re: Vista to Unix
    ... It is those exceptions that cause me to look elsewhere. ... I have used Ubuntu on this laptop before, ... It was often that the wireless networking would simply NOT ... sometimes the sleep function would ...
    (comp.os.linux.misc)
  • Re: Vista to Unix
    ... Jeffrey Drake wrote: ... It is those exceptions that cause me to look elsewhere. ... It was often that the wireless networking would simply NOT ... sometimes the sleep function would ...
    (comp.os.linux.misc)
  • Re: The Neverending Movie...
    ... what are some of these exceptions? ... The knowledge that at last we were going overseas had given the Battery the ... It was impossible to try and sleep. ... Beds crashed down in the ...
    (rec.arts.movies.current-films)
  • cant get out of infinite while loop
    ... To get into the infinite loop, I write to a file the "on" status ... writing switch on ... ready to sleep for 10 seconds ... or die "Problem passing files to the bridge directory...$!"; ...
    (comp.lang.perl.misc)