Thread Safety

S

softwareakash

Thread Safety

I wrote a small class library written in C# which writes log messages
to a log file.
Everything works fine when one ASP.net application uses this dll to
write text strings.
The problem starts when 2 or more applications ( ASP.net or Console )
uses this dll simultaneously and try to write a lot of messages (
around 10000 ).

I get error that the text file is being used by other process.
In my final application this dll will be used for logging and it should
handle unlimited number of user .

I want to make it completely thread safe so that any number of
different applications can parallaly write to the same text file.

I tried to use lock and synchronized methods etc but nothing is
working.
For writing to the text file I am using StreamWriter class.
I am using VS2005

Can anybody help?

Regards
Akash
 
M

Markus Stoeger

softwareakash said:
I get error that the text file is being used by other process.
In my final application this dll will be used for logging and it should
handle unlimited number of user .

I want to make it completely thread safe so that any number of
different applications can parallaly write to the same text file.

I tried to use lock and synchronized methods etc but nothing is
working.

lock/Monitor only works within the current process. Use the Mutex class
for interprocess synchronization.

hth,
Max
 
J

Jon Skeet [C# MVP]

I want to make it completely thread safe so that any number of
different applications can parallaly write to the same text file.

This isn't a matter of *thread* safety, it's a matter of *process*
safety. You could (as Markus mentioned) use mutexes. Another
alternative is to have a single process running which actually writes
to the files, and use sockets (or some other IPC channel) to make the
other applications talk to that process.
 
A

Alvin Bruney [MVP]

why re-invent the wheel? really? the wheel already works in the situation
you want it and it's been tested in the enterprise. That wheel is the
enterprise library logging application block. it will log simple and
efficiently and scale well across process. finally, it's a free utility
available from microsoft.

--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
 

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