| | |
File being used by another process.
Please support our C# advertiser: Intel Parallel Studio Home
![]() | View First Unread |
•
•
Join Date: Dec 2009
Posts: 1
Reputation:
Solved Threads: 0
-1
#12 •
•
•
•
I just had the same problem and
I added
GC.Collect();
GC.WaitForPendingFinalizers();
after the doc.Close(true) and that fixed the problem.
I only found that by accident. I was in debug mode with a break point after doc.Close. I noticed if I waited for a while and resumed debugging the next iteration would work sometimes. So I decided to form GC to release whatever resources that was holding the lock and it worked.
-1
#13 1 Day Ago
another alternative is to read/write files through filestreams.
Filestreams dont create a file lock so they wont tie up your files, saving you the hastle of manually calling the grabage collector
Filestreams dont create a file lock so they wont tie up your files, saving you the hastle of manually calling the grabage collector
Last edited by Ryshad; 1 Day Ago at 8:27 am.
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
•
•
Join Date: Aug 2009
Posts: 5
Reputation:
Solved Threads: 0
0
#14 1 Day Ago
Here is my final solution which seems to work quite well:
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.Text; using System.IO; namespace Toolbox { public class FileCheck { public bool Exists(string fileName) { if (File.Exists(fileName)) { Console.WriteLine("File Exists: " + fileName); return true; } return false; } public bool InUse(string fileName) { try { using (File.OpenRead(fileName)) // the using statement performs the close automatically! { return false; } } catch (IOException) { Console.WriteLine("File is in use..."); } return true; } } } There are 2 functions: FileCheck.Exists and FileCheck.InUse . . . string filePath = "FileName and Path"; int timeInSeconds = 10; // depends on how long it takes Toolbox.FileCheck fCheck = new Toolbox.FileCheck(); Toolbox.Timer timer = new Toolbox.Timer(); bool fileInUse = true; bool elapsedTimer = false; // set the timer timeout value int timeOut = timeInSeconds; // timeout in seconds timer.Start(timeOut); // check if file is in use while ((fileInUse = Check.InUse(filePath))) { if (timer.Elapsed()) { elapsedTimer = true; Console.WriteLine("Timeout occurred waiting for file: <" + filePath + "> to be release!"); break; } else { Console.WriteLine("File: <" + filePath + "> in Use..."); } System.Threading.Thread.Sleep(100); } if (elapsedTimer) { Console.WriteLine("FileInUse: Timeout, File: <" + filePath + "> is still in use!"); // here you might return file in use flag:fileInUse true } else { Console.WriteLine("File: <" + filePath + "> has been released by the Operating System."); // here you might return file in use flag: fileInUse false } . . . Here is the TimerRoutine: using System; using System.Collections.Generic; using System.Text; namespace Toolbox { public class Timer { private long _endTime; // getter/setter is called "timeOut" private long currentTime { get { return DateTime.Now.Ticks; } } private long timeOut { get { return _endTime; } set { _endTime = DateTime.Now.Ticks + new TimeSpan(0, 0, 0, (int)value).Ticks; } } public bool Start(int timeout) { timeOut = timeout; return true; } public bool Elapsed() { if (currentTime > timeOut) { return true; } else { return false; } } } }
Last edited by niek_e; 1 Day Ago at 12:04 pm. Reason: Add code-tags
0
#16 1 Day Ago
There is a small risk involved with that method; you have created a race condition. It is possible that the file is not in use when the check is made so it returns false, but the file is subsequently locked by another process.
What i've garnered from forum responses to questions like this is that the test isn't worth doing. To handle file locks you can
a) Test if file is locked before you use it
b) Try using it and handle the exception if its locked
The file system is volatile and can change between you doing the test and trying to access the record. Because the test could return a false positive you will still need to wrap your file access in try...catch blocks. If you are handling the exception anyway, then why spend time coding to perform the test?
What i've garnered from forum responses to questions like this is that the test isn't worth doing. To handle file locks you can
a) Test if file is locked before you use it
b) Try using it and handle the exception if its locked
The file system is volatile and can change between you doing the test and trying to access the record. Because the test could return a false positive you will still need to wrap your file access in try...catch blocks. If you are handling the exception anyway, then why spend time coding to perform the test?
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
0
#19 1 Day Ago
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
![]() |
Similar Threads
- Open a file. (C#)
- Saving to a file from an array in C ? (C)
- Windows programming - C - Save file function (C++)
- PLEASE HELP about Using C# to add an executable file to my program! (C#)
- Can't Delete File!!! (Windows NT / 2000 / XP)
- Help with File Reading loop (C++)
- Unable to execute a video file in browser using Java (Java)
- End of file function (C)
Other Threads in the C# Forum
- Previous Thread: Read CSV file into a 2d array
- Next Thread: Serializing? Is that the correct way to save data?
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagridview dataset datetime degrees development draganddrop drawing encryption enum excel file files form format forms ftp function gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






