The Labs \ Source Viewer \ SSCLI \ System.Threading \ EventWaitHandle

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*=============================================================================
  16. **
  17. ** Class: EventWaitHandle
  18. **
  19. **
  20. ** Purpose: Base class for representing Events
  21. **
  22. **
  23. =============================================================================*/
  24. namespace System.Threading
  25. {
  26.     using System;
  27.     using System.Threading;
  28.     using System.Runtime.CompilerServices;
  29.     using System.Security.Permissions;
  30.     using System.IO;
  31.     using Microsoft.Win32;
  32.     using Microsoft.Win32.SafeHandles;
  33.     using System.Runtime.InteropServices;
  34.     using System.Runtime.Versioning;
  35.    
  36.     [HostProtection(Synchronization = true, ExternalThreading = true)]
  37.     [ComVisibleAttribute(true)]
  38.     public class EventWaitHandle : WaitHandle
  39.     {
  40.         [ResourceExposure(ResourceScope.None)]
  41.         [ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
  42.         public EventWaitHandle(bool initialState, EventResetMode mode) : this(initialState, mode, null)
  43.         {
  44.         }
  45.        
  46.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  47.         [ResourceExposure(ResourceScope.Machine)]
  48.         [ResourceConsumption(ResourceScope.Machine)]
  49.         public EventWaitHandle(bool initialState, EventResetMode mode, string name)
  50.         {
  51.             if (null != name && System.IO.Path.MAX_PATH < name.Length) {
  52.                 throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", name));
  53.             }
  54.            
  55.             SafeWaitHandle _handle = null;
  56.             switch (mode) {
  57.                 case EventResetMode.ManualReset:
  58.                     _handle = Win32Native.CreateEvent(null, true, initialState, name);
  59.                     break;
  60.                 case EventResetMode.AutoReset:
  61.                     _handle = Win32Native.CreateEvent(null, false, initialState, name);
  62.                     break;
  63.                 default:
  64.                    
  65.                     throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag", name));
  66.                     break;
  67.             }
  68.             ;
  69.            
  70.             if (_handle.IsInvalid) {
  71.                 int errorCode = Marshal.GetLastWin32Error();
  72.                
  73.                 _handle.SetHandleAsInvalid();
  74.                 if (null != name && 0 != name.Length && Win32Native.ERROR_INVALID_HANDLE == errorCode)
  75.                     throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle", name));
  76.                
  77.                 __Error.WinIOError(errorCode, "");
  78.             }
  79.             SetHandleInternal(_handle);
  80.         }
  81.        
  82.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  83.         [ResourceExposure(ResourceScope.Machine)]
  84.         [ResourceConsumption(ResourceScope.Machine)]
  85.         public EventWaitHandle(bool initialState, EventResetMode mode, string name, out bool createdNew)
  86.         {
  87.             if (null != name && System.IO.Path.MAX_PATH < name.Length) {
  88.                 throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", name));
  89.             }
  90.             Win32Native.SECURITY_ATTRIBUTES secAttrs = null;
  91.            
  92.             SafeWaitHandle _handle = null;
  93.             bool isManualReset;
  94.             switch (mode) {
  95.                 case EventResetMode.ManualReset:
  96.                     isManualReset = true;
  97.                     break;
  98.                 case EventResetMode.AutoReset:
  99.                     isManualReset = false;
  100.                     break;
  101.                 default:
  102.                    
  103.                     throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag", name));
  104.                     break;
  105.             }
  106.             ;
  107.            
  108.             _handle = Win32Native.CreateEvent(secAttrs, isManualReset, initialState, name);
  109.             int errorCode = Marshal.GetLastWin32Error();
  110.            
  111.             if (_handle.IsInvalid) {
  112.                
  113.                 _handle.SetHandleAsInvalid();
  114.                 if (null != name && 0 != name.Length && Win32Native.ERROR_INVALID_HANDLE == errorCode)
  115.                     throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle", name));
  116.                
  117.                 __Error.WinIOError(errorCode, name);
  118.             }
  119.             createdNew = errorCode != Win32Native.ERROR_ALREADY_EXISTS;
  120.             SetHandleInternal(_handle);
  121.         }
  122.        
  123.         private EventWaitHandle(SafeWaitHandle handle)
  124.         {
  125.             SetHandleInternal(handle);
  126.         }
  127.        
  128.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  129.         [ResourceExposure(ResourceScope.Machine)]
  130.         [ResourceConsumption(ResourceScope.Machine)]
  131.         public static EventWaitHandle OpenExisting(string name)
  132.         {
  133.             if (name == null) {
  134.                 throw new ArgumentNullException("name", Environment.GetResourceString("ArgumentNull_WithParamName"));
  135.             }
  136.            
  137.             if (name.Length == 0) {
  138.                 throw new ArgumentException(Environment.GetResourceString("Argument_EmptyName"), "name");
  139.             }
  140.            
  141.             if (null != name && System.IO.Path.MAX_PATH < name.Length) {
  142.                 throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", name));
  143.             }
  144.            
  145.            
  146.             SafeWaitHandle myHandle = Win32Native.OpenEvent(Win32Native.EVENT_MODIFY_STATE | Win32Native.SYNCHRONIZE, false, name);
  147.            
  148.             if (myHandle.IsInvalid) {
  149.                 int errorCode = Marshal.GetLastWin32Error();
  150.                
  151.                 if (Win32Native.ERROR_FILE_NOT_FOUND == errorCode || Win32Native.ERROR_INVALID_NAME == errorCode)
  152.                     throw new WaitHandleCannotBeOpenedException();
  153.                 if (null != name && 0 != name.Length && Win32Native.ERROR_INVALID_HANDLE == errorCode)
  154.                     throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle", name));
  155.                 //this is for passed through Win32Native Errors
  156.                 __Error.WinIOError(errorCode, "");
  157.             }
  158.             return new EventWaitHandle(myHandle);
  159.         }
  160.         public bool Reset()
  161.         {
  162.             bool res = Win32Native.ResetEvent(safeWaitHandle);
  163.             if (!res)
  164.                 __Error.WinIOError();
  165.             return res;
  166.         }
  167.         public bool Set()
  168.         {
  169.             bool res = Win32Native.SetEvent(safeWaitHandle);
  170.            
  171.             if (!res)
  172.                 __Error.WinIOError();
  173.            
  174.             return res;
  175.         }
  176.        
  177.     }
  178. }

Developer Fusion