Home All Groups Group Topic Archive Search About

A solution to "WaitAll for multiple handles on an STA thread is not supported."

Author
30 Sep 2005 5:43 PM
isbat1
Seems like a lot of people have trouble with this error.  Here's my
solution.  I give it to the future.  Because I love you.

private void WaitAll(WaitHandle[] waitHandles) {
  if (Thread.CurrentThread.ApartmentState == ApartmentState.STA) {
    // WaitAll for multiple handles on an STA thread is not supported.
    // ...so wait on each handle individually.
    foreach(WaitHandle myWaitHandle in waitHandles) {
      WaitHandle.WaitAny(new WaitHandle[]{myWaitHandle});
    }
  }
  else {
    WaitHandle.WaitAll(waitHandles);
  }
}
Author
30 Sep 2005 6:37 PM
Jon Skeet [C# MVP]
<isb***@yahoo.com> wrote:
Show quoteHide quote
> Seems like a lot of people have trouble with this error.  Here's my
> solution.  I give it to the future.  Because I love you.
>
> private void WaitAll(WaitHandle[] waitHandles) {
>   if (Thread.CurrentThread.ApartmentState == ApartmentState.STA) {
>     // WaitAll for multiple handles on an STA thread is not supported.
>     // ...so wait on each handle individually.
>     foreach(WaitHandle myWaitHandle in waitHandles) {
>       WaitHandle.WaitAny(new WaitHandle[]{myWaitHandle});
>     }
>   }
>   else {
>     WaitHandle.WaitAll(waitHandles);
>   }
> }

That seems to change the behaviour entirely though - isn't it actually
waiting for *all* of the handles, just sequentially?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Are all your drivers up to date? click for free checkup

Author
30 Sep 2005 6:45 PM
Jon Skeet [C# MVP]
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
> That seems to change the behaviour entirely though - isn't it actually
> waiting for *all* of the handles, just sequentially?

Doh - ignore me. I thought you were trying to mimic Wait*Any* by
calling it multiple times.

Why use WaitAny with an array rather than calling WaitOne directly on
each handle? That would seem somewhat simpler to me.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
30 Sep 2005 6:48 PM
Jon Skeet [C# MVP]
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
> Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
> > That seems to change the behaviour entirely though - isn't it actually
> > waiting for *all* of the handles, just sequentially?
>
> Doh - ignore me. I thought you were trying to mimic Wait*Any* by
> calling it multiple times.
>
> Why use WaitAny with an array rather than calling WaitOne directly on
> each handle? That would seem somewhat simpler to me.

And another point (which I must admit was pointed out to me by Ian
Griffiths - I won't take credit for it) - the whole point of WaitAll is
that it's an atomic acquisition, effectively. You unfortunately lose
the atomicity in your call, so you could introduce deadlocks which
wouldn't otherwise be present.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
30 Sep 2005 6:57 PM
isbat1
Myopic thinking.  I was fixated on getting a method from WaitHandle to
work.  I blame my antibiotics.
Author
30 Sep 2005 6:52 PM
isbat1
Well, since you can't do a WaitAll from an STA thread, the only other
thing I know to do is to wait for each wait handle individually.  Have
I misunderstood something?

Bookmark and Share