jyua44
jyua44

Reputation: 1

Is there a better approach for dealing with mulitple processes reading/writing to a file

I have 3 processes each of which listens to a data feed. All the 3 processes need to read and update the same file after receiving data. The 3 processes keep running whole day. Obviously, each process needs to have exclusive read/write lock on the file.

I could use "named mutex" to protect the read/write of the file or I can open the file using FileShare.None.

Would these 2 approaches work? Which one is better?

The programe is written in C# and runs on Windows.

Upvotes: 0

Views: 164

Answers (3)

bens951
bens951

Reputation: 15

"monitor" is one of you choise since it is the solution of synchronization problem.

Upvotes: 0

Jeremy Sylvis
Jeremy Sylvis

Reputation: 46

I agree with the named mutex. Waiting / retrying on file access is very tedious and exception-prone, whereas the named mutex solution is very clean and straightforward.

Upvotes: 0

MusiGenesis
MusiGenesis

Reputation: 75296

Use a named mutex for this. If you open the file with FileShare.None in one process, the other processes will get an exception thrown when they attempt to open the file, which means you have to deal with waiting and retrying etc. in these processes.

Upvotes: 1

Related Questions