TN. TN. - 11 months ago 70
C# Question

How to find that Mutex in C# is acquired?

How can I find from mutex handle in C# that a mutex is acquired?


timeouts, it returns
. However, how can I find that from the mutex handle? (Maybe using p/invoke.)


public class InterProcessLock : IDisposable
readonly Mutex mutex;

public bool IsAcquired { get; private set; }

public InterProcessLock(string name, TimeSpan timeout)
bool created;
var security = new MutexSecurity();
security.AddAccessRule(new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.Synchronize | MutexRights.Modify, AccessControlType.Allow));
mutex = new Mutex(false, name, out created, security);
IsAcquired = mutex.WaitOne(timeout);

#region IDisposable Members

public void Dispose()
if (IsAcquired)
IsAcquired = false;


Currently, I am using my own property
to determine whether I should release a mutex. Not essential but clearer, would be not to use a secondary copy of the information represented by
property, but rather to ask directly the mutex whether it is acquired by me. Since calling
throws an exception if it is not acquired by me.

(By acquired state I mean that the mutex is in not-signaled state when I am owning the mutex.)

(EDIT: I have added
IsAcquired = false;
thanks to mattdekrey's post.)

Answer Source

As you may found, there are no public members on Mutex class:

There is also no public native functions for that:

However, there are some undocumented/unsupported functions especially in ntdll.dll. These allow accessing system objects. However, these functions may change or not be available in future versions of operating system.

So, the answer is: It is not possible using conventional means.