473,414 Members | 2,019 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Which process is locking a file ?

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

Jun 26 '06 #1
4 9584
Paddy wrote:
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

A quick search revealed the following:

http://www.codeguru.com/Cpp/W-P/dll/...hp/c3641/#more

http://ccollomb.free.fr/unlocker/

The first one has the source code (c++) included.

HTH

JB

Jun 26 '06 #2

"John B" <jb******@yahoo.com> wrote in message
news:44********@news.iprimus.com.au...
| Paddy wrote:
| > 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
| >
| A quick search revealed the following:
|
| http://www.codeguru.com/Cpp/W-P/dll/...hp/c3641/#more
|
| http://ccollomb.free.fr/unlocker/
|

Both aren't exactly what the OP is looking for, the first is to find the
DLL's locked, the second is an interactive application that requires an
operator to be present, this is not usable in the OP's user context.

Willy.

Jun 27 '06 #3

"Paddy" <pa***************@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
| 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.


Jun 27 '06 #4
Could you try an approach like this? I've never done this myself, so
take it with a grain of salt...

1) Run SysInternal's Filemon
2) When the long-running service detects the problem, have it send a
command to Filemon to suspend capturing output (i.e. CTRL-E keystroke)
3) Dump the Filemon output to a file, and search in it for the
offending app

Your service might need to be set to "interact with desktop" for this
to work and/or run under a Windows account with the proper privs.

If the size of the output is unmanageable, you might want to set the
history depth of Filemon -- I'm not sure what happens if you set it to
"no limit" and it runs for a week. Also you might want to set it up to
filter its output (is it always the same file?) and if necessary,
periodically send a keystroke to clear the output.

If you have trouble sending keystrokes for some reason, you might also
be able to simply suspend the Filemon process from your service. When
you know the problem has happened, you could resume Filemon and then
manually dump the output.

Paddy wrote:
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


Jun 27 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ram | last post by:
Hey, I have a solution that is built from both C# and VB.Net projects that some C# projects are referenced to VB.Net projects and vise versa. Whenever I try to rebuild my solution I get the...
20
by: Dave | last post by:
Hi I have been asked to implement an error logging/management database for the network technicians in our company. The idea is that staff using workstations can enter a database and log any...
5
by: Shuaib | last post by:
I wonder if anybody could shed some light on a problem I am encountering. I have written a program in C that runs on Solaris 2.8. At busy times of the day there may be multiple instances of it...
4
by: Earth Worm Jim | last post by:
I am using VS.Net 2003 on Windows 2003 Server (standard edition) and I am getting "The process cannot access the file because it is being used by another process" on DLL's in a VS.Net solution. ...
1
by: Henry Chen | last post by:
We have a web server that runs on Windows 2000/IIS 5. We have multiple site of web application on the web server. Each site has it own assembly versions of ASP.Net application. They are all...
0
by: Paul | last post by:
Hi, I'm trying to kick off the iiscnfg.vbs from a webservice to export a website's config to an xml file (And eventually populate other servers with the config). I initially tried this using the...
1
by: darrel | last post by:
Is there any tool to check to see what, at any given time, is locking a particular file? We're having an issue where some of our XML file become locked and we're tryign to figure out what is...
7
by: atlaste | last post by:
Hi, I have two different things I'd like to discuss here. Both are about cross-process synchronization of shared resources. The resources that are shared are: (1) an object writing to a file and...
9
by: zmickle | last post by:
Experts and books all say that you can share an Access back end on a shared drive with the front end running on each host computer. I have a simple database that tracks student data and it is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.