Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
isbat1@yahoo.com - 30 Sep 2005 18:43 GMT
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);
 }
}
Jon Skeet [C# MVP] - 30 Sep 2005 19:37 GMT
> 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.
[quoted text clipped - 11 lines]
>   }
> }

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

Signature

Jon Skeet - <skeet@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

Jon Skeet [C# MVP] - 30 Sep 2005 19:45 GMT
> 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.

Signature

Jon Skeet - <skeet@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

Jon Skeet [C# MVP] - 30 Sep 2005 19:48 GMT
> > That seems to change the behaviour entirely though - isn't it actually
> > waiting for *all* of the handles, just sequentially?
[quoted text clipped - 4 lines]
> 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.

Signature

Jon Skeet - <skeet@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

isbat1@yahoo.com - 30 Sep 2005 19:57 GMT
Myopic thinking.  I was fixated on getting a method from WaitHandle to
work.  I blame my antibiotics.
isbat1@yahoo.com - 30 Sep 2005 19:52 GMT
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?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.