// 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;
[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 && IsSTAThread)
+ 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");
throw new ArgumentException ("null element found", "waitHandle");
}
}
+
+ static bool IsSTAThread {
+ get {
+ bool isSTA = Thread.CurrentThread.ApartmentState ==
+ ApartmentState.STA;
+
+ // FIXME: remove this check after Thread.ApartmentState
+ // has been properly implemented.
+ if (!isSTA) {
+ Assembly asm = Assembly.GetEntryAssembly ();
+ if (asm != null)
+ isSTA = asm.EntryPoint.GetCustomAttributes (typeof (STAThreadAttribute), false).Length > 0;
+ }
+
+ return isSTA;
+ }
+ }
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)