I write my debug information to a file using a separate thread. During startup, I like to backup any previous file. Unfortunately, it seems the OS hangs onto the file handle for an indeterminate amount of time so when I try to write to the file, it fails.
I am using C#, .Net framework 3.5 on Windows XP. (Vista and Win7 have the same problem).
Here is the code that distills the problem, where t
will throw a System.IO.IOException: "The process cannot access the file 'C:\deleteMe.txt' because it is being used by another process."
public class WriteToFile {
static void Main(){
String filename=@"C:\deleteMe.txt";
String filenameBackup = @"C:\deleteMe (backup).txt";
String value = "this is a test value";
//MAKE FILE
fillFile(filename, value);
//MAKE A THREAD TO WRITE TO FILE, WHEN READY
Semaphore readyToWrite=new Semaphore(1, 1);
var t=new Thread(
new ThreadStart(delegate(){
readyToWrite.WaitOne();
WriteToFile.fillFile(filename, value);
})
);
t.Priority=ThreadPriority.Highest;
t.Start();
//BACKUP FILE
if (File.Exists(filename)) {
File.Delete(filenameBackup);
File.Copy(filename, filenameBackup);
File.Delete(filename);
}//endif
//SIGNAL THREAD TO WRITE TO FILE
readyToWrite.Release();
}//method
public static void fillFile(String filename, String value) {
try {
StreamWriter w = File.AppendText(filename);
using (w) {
w.Write(value);
w.Flush();
}//using
} catch (Exception e) {
throw new Exception("Can not write to file", e);
}//try
}//method
}//class
Thanks!