views:

197

answers:

2

I have a class which logs to a file .this class is used by multiple threads. I have use a mutex inside the write function and it works fine with one instance of my application . but if I start multiple instance of the application the it crashs.

what is the correct implementation of named mutex at class level that can work across process .

A: 

For logging I'd suggest opening (with an exclusive locK) and closing the file for every write. The lock will take care of synchronization. You might try System.IO.File.AppendAllText() method - I have a suspicion that it does exactly that.

Vilx-
+2  A: 

Use named Mutex. Works for interprocess synchronization.

Mutex mut = new Mutex(false, "Global\\uniquename");

See this post for best practice for using Mutex: http://stackoverflow.com/questions/229565/what-is-a-good-pattern-for-using-a-global-mutex-in-c

grega g