EventWaitHandle.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // System.Threading.EventWaitHandle.cs
  3. //
  4. // Author:
  5. // Dick Porter ([email protected])
  6. //
  7. // (C) Ximian, Inc. (http://www.ximian.com)
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.IO;
  30. using System.Runtime.InteropServices;
  31. #if !NET_2_1
  32. using System.Security.AccessControl;
  33. #endif
  34. namespace System.Threading
  35. {
  36. [ComVisible (true)]
  37. public class EventWaitHandle : WaitHandle
  38. {
  39. private EventWaitHandle (IntPtr handle)
  40. {
  41. Handle = handle;
  42. }
  43. private bool IsManualReset (EventResetMode mode)
  44. {
  45. if ((mode < EventResetMode.AutoReset) || (mode > EventResetMode.ManualReset))
  46. throw new ArgumentException ("mode");
  47. return (mode == EventResetMode.ManualReset);
  48. }
  49. public EventWaitHandle (bool initialState, EventResetMode mode)
  50. {
  51. bool created;
  52. bool manual = IsManualReset (mode);
  53. Handle = NativeEventCalls.CreateEvent_internal (manual, initialState, null, out created);
  54. }
  55. public EventWaitHandle (bool initialState, EventResetMode mode,
  56. string name)
  57. {
  58. bool created;
  59. bool manual = IsManualReset (mode);
  60. Handle = NativeEventCalls.CreateEvent_internal (manual, initialState, name, out created);
  61. }
  62. public EventWaitHandle (bool initialState, EventResetMode mode,
  63. string name, out bool createdNew)
  64. {
  65. bool manual = IsManualReset (mode);
  66. Handle = NativeEventCalls.CreateEvent_internal (manual, initialState, name, out createdNew);
  67. }
  68. #if !NET_2_1
  69. [MonoTODO ("Implement access control")]
  70. public EventWaitHandle (bool initialState, EventResetMode mode,
  71. string name, out bool createdNew,
  72. EventWaitHandleSecurity eventSecurity)
  73. {
  74. bool manual = IsManualReset (mode);
  75. Handle = NativeEventCalls.CreateEvent_internal (manual, initialState, name, out createdNew);
  76. }
  77. [MonoTODO]
  78. public EventWaitHandleSecurity GetAccessControl ()
  79. {
  80. throw new NotImplementedException ();
  81. }
  82. public static EventWaitHandle OpenExisting (string name)
  83. {
  84. return(OpenExisting (name, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify));
  85. }
  86. public static EventWaitHandle OpenExisting (string name, EventWaitHandleRights rights)
  87. {
  88. if (name == null) {
  89. throw new ArgumentNullException ("name");
  90. }
  91. if ((name.Length == 0) ||
  92. (name.Length > 260)) {
  93. throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
  94. }
  95. MonoIOError error;
  96. IntPtr handle = NativeEventCalls.OpenEvent_internal (name, rights, out error);
  97. if (handle == (IntPtr)null) {
  98. if (error == MonoIOError.ERROR_FILE_NOT_FOUND) {
  99. throw new WaitHandleCannotBeOpenedException (Locale.GetText ("Named Event handle does not exist: ") + name);
  100. } else if (error == MonoIOError.ERROR_ACCESS_DENIED) {
  101. throw new UnauthorizedAccessException ();
  102. } else {
  103. throw new IOException (Locale.GetText ("Win32 IO error: ") + error.ToString ());
  104. }
  105. }
  106. return(new EventWaitHandle (handle));
  107. }
  108. #endif
  109. public bool Reset ()
  110. {
  111. CheckDisposed ();
  112. return (NativeEventCalls.ResetEvent_internal (Handle));
  113. }
  114. public bool Set ()
  115. {
  116. CheckDisposed ();
  117. return (NativeEventCalls.SetEvent_internal (Handle));
  118. }
  119. #if !NET_2_1
  120. [MonoTODO]
  121. public void SetAccessControl (EventWaitHandleSecurity eventSecurity)
  122. {
  123. throw new NotImplementedException ();
  124. }
  125. #endif
  126. }
  127. }