473,700 Members | 2,500 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9610
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.c om.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.goo glegroups.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
1871
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 following errors: "Cannot Copy Assembly <Assembly> - The Process Cannot Access The File Because It Is Being Used By Another Process". I tried to "kill" the ASPNET_WP process in a pre-built event and had no success... I read in more than a few old...
20
5893
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 problems they are having, while the technicians can pull off details of faulty equipment. The users will have little experience of databases so the system needs to be customized to allow users access to facilities via simple a gui. The network is ...
5
4311
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 running (5-10), each process taking approx. 3 seconds to complete. Each instance of the program basically fetches data from a Oracle database (using Remedy ARS API routines) and stores it in a user defined structure that I have defined as a...
4
13244
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. I have made sure all the dependancies and the build order is correct but the actually instance of VS.Net (devenv.exe) is LOCKING THE DAM DLL'S IT IS TRYING TO BUILD. ????? I have used Process Explorer from sysinternals.com to confirm this.
1
1681
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 developed with ASP.Net 1.0. When we test a site of the application, it sometimes pops up stating that a file is locked another process and can not be accessed. I try to stop the ASPNET_WP process via task manager on the web server, the service will start...
0
7213
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.1 framework, but discovered that when I used Process.Start() the user's credentials were not impersonated, it just kept using the asp.net's user, so I switched the webservice over to 2.0 as Diagnostics.Process.StartInfo now has username,...
1
1198
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 locking them. -Darrel
7
8836
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 (2) a file. I threat them as two different problems for various reasons. The first problem involves the object writing to a file. The problem might be best explained by thinking of a single log file that's appended by different programs that...
9
2600
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 shared between 4 staff memebers. Each staff computer has a copy of the front-end (linked tables, forms, and queries). They basically only use one form. The form works like this
0
8726
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8645
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9214
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8925
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7810
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5903
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4657
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3089
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2028
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.