When any process opens a file, it is automatically locked until it is
closed. This is usually for only a few milliseconds, if your programs are
working with the files correctly (that is, closing them as soon as
possible).
If you want to be sure that the file is not in use, just put the code to
open it in a try/catch block inside a loop. Then you can put the thread to
sleep for a few milliseconds each time through the loop (in the Catch block)
when the file can't be opened. Be sure to include a "time-out" routine to
prevent the loop from executing infinitely.
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big things are made up of
lots of little things.
"Ron Vecchi" <ve******@comcast.net> wrote in message
news:up**************@TK2MSFTNGP09.phx.gbl...
I have a single line file where the line will be read and updated each
time it is opened.
My question is I need to open it with a lock so noone else can read or
write to it untill the lock is released.
The problem seems that what If it is locked and A different request tries
to open it. From what I understand an exception will be thrown.
It is critical that each request reads the file. Is there an easy way to
make a request wait untill the file can be opened then read from it with
out generating the exception.
Thanks,
Ron Vecchi