Opening a file (log file) for write only but need a lock on it.

U

UJ

What's the easiest/best way to open a single file from multiple applications
but so only one can read it at a time? I tried a mutex but had problems. I
noticed there is something called a ReaderWriter class but it says it's best
for reads.

I'll never be reading the file.....

TIA - Jeff.
 
G

Greg Young

If you want to keep the file open and they are all on the same machine your
initial thought of using a mutex seems correct ...

What were your problems with using a mutex to define a critical section when
writing to the file?

Cheers,

Greg
 
U

UJ

Every once in a while the mutex seemed to get lost by somebody so that then
nobody could access to the file.

Of course, since then I've cleaned up the code a lot so it may be fixed now.
I just have tried it.

I guess unless somebody else has a better idea, I'll put the mutex back in
and try it to see what would happen.

Thanks.
 
U

UJ

What about using the lock keyword?

Do something like

lock this
{
OpenFile
WriteToFile
CloseFile
}

I'll be using the same object everywhere to do the actual writing....

J.
 
G

Greg Young

"What's the easiest/best way to open a single file from multiple
applications "

You said that you were using multiple applications ... lock() only works
within the given appdomain (except in one odd circumstance which I highly
doubt you are using) .. It will not work between multiple processes ever as
the processes cannot possible have access to the same instance, remoting may
make you think you have access to the same object but in reality each app
domain has its own unique proxy (which is what would have to be locked on).

Cheers,

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top