2 // System.Threading.WaitHandle.cs
5 // Dick Porter (dick@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
10 using System.Runtime.CompilerServices;
12 namespace System.Threading
14 public abstract class WaitHandle : MarshalByRefObject, IDisposable
16 [MethodImplAttribute(MethodImplOptions.InternalCall)]
17 private static extern bool WaitAll_internal(WaitHandle[] handles, int ms, bool exitContext);
19 public static bool WaitAll(WaitHandle[] waitHandles) {
20 if(waitHandles.Length>64) {
21 throw new NotSupportedException("Too many handles");
23 for(int i=0; i<waitHandles.Length; i++) {
24 if(waitHandles[i]==null) {
25 throw new ArgumentNullException("null handle");
29 return(WaitAll_internal(waitHandles, Timeout.Infinite,
33 public static bool WaitAll(WaitHandle[] waitHandles,
34 int millisecondsTimeout,
36 if(waitHandles.Length>64) {
37 throw new NotSupportedException("Too many handles");
39 for(int i=0; i<waitHandles.Length; i++) {
40 if(waitHandles[i]==null) {
41 throw new ArgumentNullException("null handle");
45 return(WaitAll_internal(waitHandles, millisecondsTimeout, false));
48 public static bool WaitAll(WaitHandle[] waitHandles,
51 if(timeout.Milliseconds < 0 ||
52 timeout.Milliseconds > Int32.MaxValue) {
53 throw new ArgumentOutOfRangeException("Timeout out of range");
55 if(waitHandles.Length>64) {
56 throw new NotSupportedException("Too many handles");
58 for(int i=0; i<waitHandles.Length; i++) {
59 if(waitHandles[i]==null) {
60 throw new ArgumentNullException("null handle");
64 return(WaitAll_internal(waitHandles,
69 [MethodImplAttribute(MethodImplOptions.InternalCall)]
70 private static extern int WaitAny_internal(WaitHandle[] handles, int ms, bool exitContext);
72 // LAMESPEC: Doesn't specify how to signal failures
73 public static int WaitAny(WaitHandle[] waitHandles) {
74 if(waitHandles.Length>64) {
75 throw new NotSupportedException("Too many handles");
77 for(int i=0; i<waitHandles.Length; i++) {
78 if(waitHandles[i]==null) {
79 throw new ArgumentNullException("null handle");
83 return(WaitAny_internal(waitHandles, Timeout.Infinite,
87 public static int WaitAny(WaitHandle[] waitHandles,
88 int millisecondsTimeout,
90 if(waitHandles.Length>64) {
91 throw new NotSupportedException("Too many handles");
93 for(int i=0; i<waitHandles.Length; i++) {
94 if(waitHandles[i]==null) {
95 throw new ArgumentNullException("null handle");
99 return(WaitAny_internal(waitHandles,
104 public static int WaitAny(WaitHandle[] waitHandles,
105 TimeSpan timeout, bool exitContext) {
106 if(timeout.Milliseconds < 0 ||
107 timeout.Milliseconds > Int32.MaxValue) {
108 throw new ArgumentOutOfRangeException("Timeout out of range");
110 if(waitHandles.Length>64) {
111 throw new NotSupportedException("Too many handles");
113 for(int i=0; i<waitHandles.Length; i++) {
114 if(waitHandles[i]==null) {
115 throw new ArgumentNullException("null handle");
119 return(WaitAny_internal(waitHandles,
120 timeout.Milliseconds,
125 public WaitHandle() {
129 public const int WaitTimeout = 258;
131 protected IntPtr os_handle = IntPtr.Zero;
133 public virtual IntPtr Handle {
143 public virtual void Close() {
145 GC.SuppressFinalize (this);
148 [MethodImplAttribute(MethodImplOptions.InternalCall)]
149 protected virtual extern bool WaitOne_internal(IntPtr handle, int ms, bool exitContext);
151 public virtual bool WaitOne() {
152 return(WaitOne_internal(os_handle, Timeout.Infinite,
156 public virtual bool WaitOne(int millisecondsTimeout, bool exitContext) {
157 return(WaitOne_internal(os_handle,
162 public virtual bool WaitOne(TimeSpan timeout, bool exitContext) {
163 return(WaitOne_internal(os_handle,
164 timeout.Milliseconds,
168 protected static readonly IntPtr InvalidHandle;
170 private bool disposed = false;
172 void IDisposable.Dispose() {
174 // Take yourself off the Finalization queue
175 GC.SuppressFinalize(this);
178 protected virtual void Dispose(bool explicitDisposing) {
179 // Check to see if Dispose has already been called.
182 // If this is a call to Dispose,
183 // dispose all managed resources.
184 if(explicitDisposing) {
185 // Free up stuff here
186 //Components.Dispose();
189 // Release unmanaged resources
190 // Note that this is not thread safe.
191 // Another thread could start
192 // disposing the object after the
193 // managed resources are disposed, but
194 // before the disposed flag is set to
197 //handle=IntPtr.Zero;