I have a process that needs to read and write to a file. The application has a specific order to its reads and writes and I want to preserve this order. What I would like to do is implement something that lets the first operation start and makes the second operation wait until the first is done with a first come first served like of queue to access the file. From what I have read file locking seems like it might be what I am looking for but I have not been able to find a very good example. Can anyone provide one?
Currently I am using a TextReader/Writer with .Synchronized but this is not doing what I hoped it would.
Sorry if this is a very basic question, threading gives me a headache :S
It should be as simple as this:
Basically, define a lock object, then whenever you need to do something with your file, make sure you get a lock using the C#
lockkeyword. On reaching thelockstatement, execution will block indefinitely until a lock has been obtained.There are other constructs you can use for locking, but I find the
lockkeyword to be the most straightforward.