System.Threading.Semaphore.OpenExisting C# (CSharp) Метод

OpenExisting() приватный Метод

private OpenExisting ( string name ) : Semaphore
name string
Результат Semaphore
        public static Semaphore OpenExisting(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if(name.Length  == 0)
            {
                throw new ArgumentException(SR.GetString(SR.InvalidNullEmptyArgument, "name"), "name");
            }
            if(null != name && MAX_PATH < name.Length)
            {
                throw new ArgumentException(SR.GetString(SR.Argument_WaitHandleNameTooLong));
            }
            //Pass false to OpenSemaphore to prevent inheritedHandles
            SafeWaitHandle myHandle = SafeNativeMethods.OpenSemaphore(NativeMethods.SEMAPHORE_MODIFY_STATE | NativeMethods.SYNCHRONIZE, false, name);
            
            if (myHandle.IsInvalid)
            {
                int errorCode = Marshal.GetLastWin32Error();

                if (NativeMethods.ERROR_FILE_NOT_FOUND == errorCode || NativeMethods.ERROR_INVALID_NAME == errorCode)
                    throw new WaitHandleCannotBeOpenedException();
                if (null != name && 0 != name.Length && NativeMethods.ERROR_INVALID_HANDLE == errorCode)
                    throw new WaitHandleCannotBeOpenedException(SR.GetString(SR.WaitHandleCannotBeOpenedException_InvalidHandle,name));
                //this is for passed through NativeMethods Errors
                InternalResources.WinIOError();
            }
            return new Semaphore(myHandle);
        }

Usage Example

Пример #1
0
 /// <summary>Opens an existing named semaphore.</summary>
 /// <returns>A <see cref="T:System.Threading.Semaphore" /> object that represents a named system semaphore.</returns>
 /// <param name="name">The name of a named system semaphore.</param>
 /// <exception cref="T:System.ArgumentException">
 ///   <paramref name="name" /> is an empty string.-or-<paramref name="name" /> is longer than 260 characters.</exception>
 /// <exception cref="T:System.ArgumentNullException">
 ///   <paramref name="name" /> is null.</exception>
 /// <exception cref="T:System.Threading.WaitHandleCannotBeOpenedException">The named semaphore does not exist.</exception>
 /// <exception cref="T:System.IO.IOException">A Win32 error occurred.</exception>
 /// <exception cref="T:System.UnauthorizedAccessException">The named semaphore exists, but the user does not have the security access required to use it.</exception>
 /// <filterpriority>1</filterpriority>
 /// <PermissionSet>
 ///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode" />
 /// </PermissionSet>
 public static Semaphore OpenExisting(string name)
 {
     return(Semaphore.OpenExisting(name, System.Security.AccessControl.SemaphoreRights.Modify | System.Security.AccessControl.SemaphoreRights.Synchronize));
 }
All Usage Examples Of System.Threading.Semaphore::OpenExisting