// Gonzalo Paniagua Javier (gonzalo@ximian.com
//
// (C) 2002,2003 Ximian, Inc. (http://www.ximian.com)
-//
-
-//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
+using System;
+using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Remoting.Contexts;
+using System.Security.Permissions;
namespace System.Threading
{
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern bool WaitAll_internal(WaitHandle[] handles, int ms, bool exitContext);
- static void CheckArray (WaitHandle [] handles)
+ static void CheckArray (WaitHandle [] handles, bool waitAll)
{
if (handles == null)
throw new ArgumentNullException ("waitHandles");
if (length > 64)
throw new NotSupportedException ("Too many handles");
+ if (waitAll && length > 1 &&
+ (Thread.CurrentThread.ApartmentState == ApartmentState.STA ||
+ Assembly.GetEntryAssembly ().EntryPoint.GetCustomAttributes (typeof (STAThreadAttribute), false).Length == 1))
+ throw new NotSupportedException ("WaitAll for multiple handles is not allowed on an STA thread.");
+
foreach (WaitHandle w in handles) {
if (w == null)
throw new ArgumentNullException ("waitHandles", "null handle");
public static bool WaitAll(WaitHandle[] waitHandles)
{
- CheckArray (waitHandles);
+ CheckArray (waitHandles, true);
return(WaitAll_internal(waitHandles, Timeout.Infinite, false));
}
public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext)
{
- CheckArray (waitHandles);
+ CheckArray (waitHandles, true);
try {
if (exitContext) SynchronizationAttribute.ExitContext ();
return(WaitAll_internal(waitHandles, millisecondsTimeout, false));
TimeSpan timeout,
bool exitContext)
{
- CheckArray (waitHandles);
+ CheckArray (waitHandles, true);
long ms = (long) timeout.TotalMilliseconds;
if (ms < -1 || ms > Int32.MaxValue)
// LAMESPEC: Doesn't specify how to signal failures
public static int WaitAny(WaitHandle[] waitHandles)
{
- CheckArray (waitHandles);
+ CheckArray (waitHandles, false);
return(WaitAny_internal(waitHandles, Timeout.Infinite, false));
}
int millisecondsTimeout,
bool exitContext)
{
- CheckArray (waitHandles);
+ CheckArray (waitHandles, false);
try {
if (exitContext) SynchronizationAttribute.ExitContext ();
return(WaitAny_internal(waitHandles, millisecondsTimeout, exitContext));
public static int WaitAny(WaitHandle[] waitHandles,
TimeSpan timeout, bool exitContext)
{
- CheckArray (waitHandles);
+ CheckArray (waitHandles, false);
long ms = (long) timeout.TotalMilliseconds;
if (ms < -1 || ms > Int32.MaxValue)
return(os_handle);
}
+ [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
+ [SecurityPermission (SecurityAction.InheritanceDemand, UnmanagedCode = true)]
set {
os_handle=value;
}
protected static readonly IntPtr InvalidHandle = IntPtr.Zero;
- private bool disposed = false;
+ bool disposed = false;
void IDisposable.Dispose() {
Dispose(true);