Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, if 2 processes are reading and writing to the same file, what is the best way to avoid process locking exceptions?

Tags:

With the following file reading code:

using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None)) {     using (TextReader tr = new StreamReader(fileStream))     {         string fileContents = tr.ReadToEnd();     } } 

And the following file write code:

using (TextWriter tw = new StreamWriter(fileName)) {     tw.Write(fileContents);     tw.Close(); } 

The following exception details are seen:

The process cannot access the file 'c:\temp\myfile.txt' because it is being used by another process.

What is the best way of avoiding this? Does the reader need to retry upon receipt of the exception or is there some better way?

Note that the reader process is using a FileSystemWatcher to know when the file has changed.

Also note that, in this instance, I'm not looking for alternatives ways of sharing strings between the 2 processes.

like image 301
Iain Avatar asked Oct 21 '08 14:10

Iain


People also ask

What does || mean in C?

The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise.

What is ?: operator in C?

In other words, we can also say that an operator is a symbol that tells the compiler to perform specific mathematical, conditional, or logical functions. It is a symbol that operates on a value or a variable. For example, + and - are the operators to perform addition and subtraction in any C program.

Is an operator in C?

Summary. An operator is a symbol which operates on a variable or value. There are types of operators like arithmetic, logical, conditional, relational, bitwise, assignment operators etc. Some special types of operators are also present in C like sizeof(), Pointer operator, Reference operator etc.

What is the use of in C?

In C/C++, the # sign marks preprocessor directives. If you're not familiar with the preprocessor, it works as part of the compilation process, handling includes, macros, and more.


2 Answers

You can open a file for writing and only lock write access, thereby allowing others to still read the file.

For example,

using (FileStream stream = new FileStream(@"C:\Myfile.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.Read)) {    // Do your writing here. } 

Other file access just opens the file for reading and not writing, and allows readwrite sharing.

using (FileStream stream = new FileStream(@"C:\Myfile.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {    // Does reading  here. } 

If you want to ensure that readers will always read an up-to-date file, you will either need to use a locking file that indicates someone is writing to the file (though you may get a race condition if not carefully implemented) or make sure you block write-sharing when opening to read and handle the exception so you can try again until you get exclusive access.

like image 139
Jeff Yates Avatar answered Sep 17 '22 21:09

Jeff Yates


If you create a named Mutex you can define the mutex in the writing application, and have the reading application wait until the mutex is released.

So in the notification process that is currently working with the FileSystemWatcher, simply check to see if you need to wait for the mutex, if you do, it will wait, then process.

Here is a VB example of a Mutex like this that I found, it should be easy enough to convert to C#.

like image 41
Mitchel Sellers Avatar answered Sep 19 '22 21:09

Mitchel Sellers