Re: Which process is locking a file ?
- From: "Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx>
- Date: Tue, 27 Jun 2006 12:04:35 +0200
"Paddy" <padmanabha.kamath@xxxxxxxxx> wrote in message
news:1151333034.318399.137430@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
| We have a recurring problem where a (long-running) service throws an
| exception while trying to access a file. The problem is that this is
| very rare (happens once every week and without warning) but does happen
| time to time. We are trying ways to find which other process is holding
| on to the file or even if any other thread from our process is holding
| on to the file at the specific time that the problem occurs.
|
| So, I am trying to invesitigate whether there are any ways that when we
| throw an exception that "The file is in use ...", specifically WHICH
| process is holding on to it. i.e. which process is locking the file or
| using it. As I was saying, this is a rarely occuring phenomenon and
| Process Explorer is not of much use to us as it (as far as I know) does
| not have a monitoring mode.
|
| Can anyone please let me know how this can be accomplished in code (C#,
| C, C++ .... doesn't matter) ?
|
| - K
|
It's nearly impossible in real-time, to know which other process/thread has
the file open in Windows. The only way to find the process who has a file
open is by inspecting each process handle table (by taking a snapshot like
Process Explorer does), this is an expensive (time consuming) operation
which requires elevated privileges (not something you want your service to
run with), by the time you have taken a snapshot of THE process, it's
possible that the handle has been released.
If the file is shareable across processes, there is very little you can do,
other than retrying to open the file. If the file can only be shared across
your process threads, you have a design flaw.
If the file must be shared across threads you should have set the correct
sharing mode (ReadWrite), on the other hand, if the file should not be
shared across threads, then you should synchronize the file accesses, or
better, have only one thread accessing the file.
Willy.
.
- References:
- Which process is locking a file ?
- From: Paddy
- Which process is locking a file ?
- Prev by Date: Re: هام جدا وشكراا
- Next by Date: Re: Embed Another Application In my C# App
- Previous by thread: Re: Which process is locking a file ?
- Next by thread: Re: Which process is locking a file ?
- Index(es):
Relevant Pages
|
Loading