using System.Runtime.CompilerServices;
using System.Runtime.Remoting.Contexts;
using System.Security.Permissions;
-
-#if NET_2_0
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.Runtime.ConstrainedExecution;
-#endif
namespace System.Threading
{
-#if NET_2_0
[ComVisible (true)]
-#endif
public abstract class WaitHandle : MarshalByRefObject, IDisposable
{
[MethodImplAttribute(MethodImplOptions.InternalCall)]
if (length > 64)
throw new NotSupportedException ("Too many handles");
+ if (handles.Length == 0) {
+ // MS throws different exceptions from the different methods.
+ if (waitAll)
+ throw new ArgumentNullException ("waitHandles");
+ else
+ throw new ArgumentException ();
+ }
+
#if false
//
// Although we should thrown an exception if this is an STA thread,
if (w == null)
throw new ArgumentNullException ("waitHandles", "null handle");
-#if NET_2_0
if (w.safe_wait_handle == null)
throw new ArgumentException ("null element found", "waitHandle");
-#else
- if (w.os_handle == InvalidHandle)
- throw new ArgumentException ("null element found", "waitHandle");
-#endif
+
}
}
-
+#if false
+ // usage of property is commented - see above
static bool IsSTAThread {
get {
bool isSTA = Thread.CurrentThread.ApartmentState ==
return isSTA;
}
}
-
+#endif
public static bool WaitAll(WaitHandle[] waitHandles)
{
CheckArray (waitHandles, true);
public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext)
{
CheckArray (waitHandles, true);
+ // check negative - except for -1 (which is Timeout.Infinite)
+ if (millisecondsTimeout < Timeout.Infinite)
+ throw new ArgumentOutOfRangeException ("millisecondsTimeout");
+
try {
if (exitContext) SynchronizationAttribute.ExitContext ();
return(WaitAll_internal(waitHandles, millisecondsTimeout, false));
private static extern int WaitAny_internal(WaitHandle[] handles, int ms, bool exitContext);
// LAMESPEC: Doesn't specify how to signal failures
-#if NET_2_0
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
-#endif
public static int WaitAny(WaitHandle[] waitHandles)
{
CheckArray (waitHandles, false);
return(WaitAny_internal(waitHandles, Timeout.Infinite, false));
}
-#if NET_2_0
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
-#endif
public static int WaitAny(WaitHandle[] waitHandles,
int millisecondsTimeout,
bool exitContext)
{
CheckArray (waitHandles, false);
+ // check negative - except for -1 (which is Timeout.Infinite)
+ if (millisecondsTimeout < Timeout.Infinite)
+ throw new ArgumentOutOfRangeException ("millisecondsTimeout");
+
try {
if (exitContext) SynchronizationAttribute.ExitContext ();
return(WaitAny_internal(waitHandles, millisecondsTimeout, exitContext));
}
}
-#if NET_2_0
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
-#endif
+ public static int WaitAny(WaitHandle[] waitHandles, TimeSpan timeout)
+ {
+ return WaitAny (waitHandles, timeout, false);
+ }
+
+ [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
+ public static int WaitAny(WaitHandle[] waitHandles, int millisecondsTimeout)
+ {
+ return WaitAny (waitHandles, millisecondsTimeout, false);
+ }
+
+ [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
public static int WaitAny(WaitHandle[] waitHandles,
TimeSpan timeout, bool exitContext)
{
}
}
- [MonoTODO]
-#if NET_2_0
- protected
-#else
- public
-#endif
- WaitHandle() {
+ protected WaitHandle()
+ {
// FIXME
}
GC.SuppressFinalize (this);
}
+#if NET_4_0
+ public void Dispose ()
+#else
+ void IDisposable.Dispose ()
+#endif
+ {
+ Close ();
+ }
+
public const int WaitTimeout = 258;
-#if NET_2_0
//
// In 2.0 we use SafeWaitHandles instead of IntPtrs
//
}
}
- [MonoTODO]
public static bool SignalAndWait (WaitHandle toSignal,
WaitHandle toWaitOn)
{
- throw new NotImplementedException ();
+ return SignalAndWait (toSignal, toWaitOn, -1, false);
}
- [MonoTODO]
public static bool SignalAndWait (WaitHandle toSignal,
WaitHandle toWaitOn,
int millisecondsTimeout,
bool exitContext)
{
- throw new NotImplementedException ();
+ if (toSignal == null)
+ throw new ArgumentNullException ("toSignal");
+ if (toWaitOn == null)
+ throw new ArgumentNullException ("toWaitOn");
+
+ if (millisecondsTimeout < -1)
+ throw new ArgumentOutOfRangeException ("millisecondsTimeout");
+
+ return SignalAndWait_Internal (toSignal.Handle, toWaitOn.Handle, millisecondsTimeout, exitContext);
}
- [MonoTODO]
public static bool SignalAndWait (WaitHandle toSignal,
WaitHandle toWaitOn,
TimeSpan timeout,
bool exitContext)
{
- throw new NotImplementedException ();
+ double ms = timeout.TotalMilliseconds;
+ if (ms > Int32.MaxValue)
+ throw new ArgumentOutOfRangeException ("timeout");
+
+ return SignalAndWait (toSignal, toWaitOn, Convert.ToInt32 (ms), false);
}
+ [MethodImplAttribute(MethodImplOptions.InternalCall)]
+ static extern bool SignalAndWait_Internal (IntPtr toSignal, IntPtr toWaitOn, int ms, bool exitContext);
+
public virtual bool WaitOne()
{
CheckDisposed ();
public virtual bool WaitOne(int millisecondsTimeout, bool exitContext)
{
CheckDisposed ();
+ // check negative - except for -1 (which is Timeout.Infinite)
+ if (millisecondsTimeout < Timeout.Infinite)
+ throw new ArgumentOutOfRangeException ("millisecondsTimeout");
+
bool release = false;
try {
if (exitContext)
}
}
+ public virtual bool WaitOne (int millisecondsTimeout)
+ {
+ return WaitOne (millisecondsTimeout, false);
+ }
+
+ public virtual bool WaitOne (TimeSpan timeout)
+ {
+ return WaitOne (timeout, false);
+ }
+
public virtual bool WaitOne(TimeSpan timeout, bool exitContext)
{
CheckDisposed ();
if (disposed || safe_wait_handle == null)
throw new ObjectDisposedException (GetType ().FullName);
}
-#else
- private IntPtr os_handle = InvalidHandle;
-
- public virtual IntPtr Handle {
- get {
- return(os_handle);
- }
-
- [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
- [SecurityPermission (SecurityAction.InheritanceDemand, UnmanagedCode = true)]
- set {
- os_handle=value;
- }
- }
- internal void CheckDisposed ()
+ public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout)
{
- if (disposed || os_handle == InvalidHandle)
- throw new ObjectDisposedException (GetType ().FullName);
+ return WaitAll (waitHandles, millisecondsTimeout, false);
}
-
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern bool WaitOne_internal(IntPtr handle, int ms, bool exitContext);
-
- protected virtual void Dispose(bool explicitDisposing) {
- // Check to see if Dispose has already been called.
- if (!disposed) {
- disposed=true;
- if (os_handle == InvalidHandle)
- return;
- lock (this) {
- if (os_handle != InvalidHandle) {
- NativeEventCalls.CloseEvent_internal (os_handle);
- os_handle = InvalidHandle;
- }
- }
- }
- }
-
- public virtual bool WaitOne()
+ public static bool WaitAll(WaitHandle[] waitHandles, TimeSpan timeout)
{
- CheckDisposed ();
- return(WaitOne_internal(os_handle, Timeout.Infinite, false));
+ return WaitAll (waitHandles, timeout, false);
}
-
- public virtual bool WaitOne(int millisecondsTimeout, bool exitContext)
- {
- CheckDisposed ();
- try {
- if (exitContext) SynchronizationAttribute.ExitContext ();
- return(WaitOne_internal(os_handle, millisecondsTimeout, exitContext));
- }
- finally {
- if (exitContext) SynchronizationAttribute.EnterContext ();
- }
- }
-
- public virtual bool WaitOne(TimeSpan timeout, bool exitContext)
- {
- CheckDisposed ();
- long ms = (long) timeout.TotalMilliseconds;
- if (ms < -1 || ms > Int32.MaxValue)
- throw new ArgumentOutOfRangeException ("timeout");
-
- try {
- if (exitContext) SynchronizationAttribute.ExitContext ();
- return (WaitOne_internal(os_handle, (int) ms, exitContext));
- }
- finally {
- if (exitContext) SynchronizationAttribute.EnterContext ();
- }
- }
-#endif
-
+
protected static readonly IntPtr InvalidHandle = (IntPtr) (-1);
bool disposed = false;
- void IDisposable.Dispose() {
- Dispose(true);
- // Take yourself off the Finalization queue
- GC.SuppressFinalize(this);
- }
-
~WaitHandle() {
Dispose(false);
}