Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

What process is holding a file?

Posted on 2004-04-07
4
Medium Priority
?
1,066 Views
Last Modified: 2010-08-05
Does anyone know how to find out what process is holding a file?
 
My program has to parse *.txt file after file has been copied to "C:\temp\" folder
The problem is that FileSystemWatcher.OnCreated evet fires the moment file is created (while still it is being copied)

I get an error "Additional information: The process cannot access the file because it is being used by another process."

Dim watcher As New FileSystemWatcher

watcher.Filter = "*.txt"
watcher.Path = "C:\temp\"
AddHandler watcher.Created, AddressOf OnCreated
watcher.EnableRaisingEvents = True

Private Shared Sub OnCreated(ByVal source As Object, ByVal e As FileSystemEventArgs)
   'do some parsing....
End Sub
0
Comment
Question by:ilexas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 10775233
I don't think you need to find the process which holds the file. All you need is to wait while file access is denied. You can create worker thread for each file and try to open file in this thread. If you get "Access denied", sleep some time and try again. Define some maximal time for this loop. By doing this you can exclude the case when file is created and kept by some program for a long time.
0
 
LVL 1

Author Comment

by:ilexas
ID: 10775411
How can I "wait while file access is denied" ? how While loop would look lite? can you give me a code example?
Thanks
0
 
LVL 22

Accepted Solution

by:
_TAD_ earned 1500 total points
ID: 10775564

While I agree with Alex, I would add an extra step.

Catching errors is a rather expensive feature, and using the "Catch" feature to handle known/expected errors is not a good habit to get in to.

I would do exactly what Alex suggests, but I would check the creation date and time first, and not access it if the time is within say... 15 seconds of current time.



something like this:

using System;
using System.Threading;


{psedo code}

<FileSystemWatcher Event -- Found a file!>

Get the file's creation time
if fileDateTime + 15 seconds > GetDateTime.Now Then
     Thread.Sleep((GetDateTime.Now - fileDateTime + 15) * 1000) // so it never waits more than 15 seconds

while(true)
{
   int cnt = 0;
   try
   {
       DoWork();
       break;  // jumps out of while loop
   }
   catch
   {
       if (++cnt < 5)
           Thread.Sleep(5000); // sleep for 5 seconds    
       else
             Throw New Error --- file too busy
   }
}

0
 
LVL 22

Expert Comment

by:_TAD_
ID: 10775579
try
   {
       DoWork();
...
..
.


the DoWork() function is where you handle your file... open, move, delete.. whatever.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question