By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,710 Members | 1,371 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,710 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Sep 11 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
"UJ" <fr**@nowhere.comwrote in message
news:ua**************@TK2MSFTNGP05.phx.gbl...
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.


Sep 11 '06 #2

P: n/a
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.

"Greg Young" <dr*******************@hotmail.comwrote in message
news:O4**************@TK2MSFTNGP05.phx.gbl...
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
"UJ" <fr**@nowhere.comwrote in message
news:ua**************@TK2MSFTNGP05.phx.gbl...
>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.



Sep 11 '06 #3

P: n/a
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.

"UJ" <fr**@nowhere.comwrote in message
news:e5**************@TK2MSFTNGP03.phx.gbl...
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.

"Greg Young" <dr*******************@hotmail.comwrote in message
news:O4**************@TK2MSFTNGP05.phx.gbl...
>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
"UJ" <fr**@nowhere.comwrote in message
news:ua**************@TK2MSFTNGP05.phx.gbl...
>>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.




Sep 11 '06 #4

P: n/a
"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

"UJ" <fr**@nowhere.comwrote in message
news:u4**************@TK2MSFTNGP04.phx.gbl...
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.

"UJ" <fr**@nowhere.comwrote in message
news:e5**************@TK2MSFTNGP03.phx.gbl...
>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.

"Greg Young" <dr*******************@hotmail.comwrote in message
news:O4**************@TK2MSFTNGP05.phx.gbl...
>>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
"UJ" <fr**@nowhere.comwrote in message
news:ua**************@TK2MSFTNGP05.phx.gbl...
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.




Sep 11 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.