System.Threading.Mutex.Mutex C# (CSharp) Method

Mutex() private method

private Mutex ( bool initiallyOwned, String name, bool &createdNew ) : System
initiallyOwned bool
name String
createdNew bool
return System
        public Mutex(bool initiallyOwned, String name, out bool createdNew)
        {
            if(null != name && System.IO.Path.MAX_PATH < name.Length)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong",name));
            }            
            Win32Native.SECURITY_ATTRIBUTES secAttrs = null;
            SafeWaitHandle mutexHandle = null;
            bool newMutex = false;
            RuntimeHelpers.CleanupCode cleanupCode = new RuntimeHelpers.CleanupCode(MutexCleanupCode);
            MutexCleanupInfo cleanupInfo = new MutexCleanupInfo(mutexHandle, false);            
            RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(
                delegate(object userData)  {  // try block                
                    RuntimeHelpers.PrepareConstrainedRegions();
                    try {
                    }
                    finally {
                        if (initiallyOwned) {
                            cleanupInfo.inCriticalRegion = true;
                            Thread.BeginThreadAffinity();
                            Thread.BeginCriticalRegion();
                        }
                    }

                    int errorCode = 0;                    
                    RuntimeHelpers.PrepareConstrainedRegions();
                    try {
                    }
                    finally {
                        errorCode = CreateMutexHandle(initiallyOwned, name, secAttrs, out mutexHandle);
                    }                    

                    if (mutexHandle.IsInvalid) {
                        mutexHandle.SetHandleAsInvalid();
                        if(null != name && 0 != name.Length && Win32Native.ERROR_INVALID_HANDLE == errorCode)
                            throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle", name));
                        __Error.WinIOError(errorCode, name);
                    }
                    newMutex = errorCode != Win32Native.ERROR_ALREADY_EXISTS;
                    SetHandleInternal(mutexHandle);
                    mutexHandle.SetAsMutex();

                    hasThreadAffinity = true;

                },
                cleanupCode,
                cleanupInfo);             
                createdNew = newMutex;

}

Same methods

Mutex::Mutex ( ) : System
Mutex::Mutex ( SafeWaitHandle handle ) : System
Mutex::Mutex ( bool initiallyOwned ) : System
Mutex::Mutex ( bool initiallyOwned, String name ) : System