1

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 .

Steve Gilham
  • 11,237
  • 3
  • 31
  • 37

2 Answers2

5

Use named Mutex. Works for interprocess synchronization.

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

See this post for best practice for using Mutex: What is a good pattern for using a Global Mutex in C#?

Community
  • 1
  • 1
grega g
  • 1,079
  • 1
  • 12
  • 25
0

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-
  • 104,512
  • 87
  • 279
  • 422