G
Guest
I have written a dll that performs file IO that will be called frequently. I
have been running into problems where file locking is occuring when a thread
cannot access the file since it is being used. How do I lock this IO
operation down while it is being used and tell the other threads to wait
until the IO is complete - like waiting in a checkout line? I have looked
into numerous examples, but just cannot find anything that relates to file IO
that works.
lock(this)
{
System.Threading.Monitor.Enter(this);
FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate,
FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("etcetc");
sw.Flush();
sw.Close();
System.Threading.Monitor.Pulse(this);
System.Threading.Monitor.Exit(this);
}
Any help is appreciated, I am new to threading.
have been running into problems where file locking is occuring when a thread
cannot access the file since it is being used. How do I lock this IO
operation down while it is being used and tell the other threads to wait
until the IO is complete - like waiting in a checkout line? I have looked
into numerous examples, but just cannot find anything that relates to file IO
that works.
lock(this)
{
System.Threading.Monitor.Enter(this);
FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate,
FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("etcetc");
sw.Flush();
sw.Close();
System.Threading.Monitor.Pulse(this);
System.Threading.Monitor.Exit(this);
}
Any help is appreciated, I am new to threading.