By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,690 Members | 1,494 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,690 IT Pros & Developers. It's quick & easy.

Can't WaitHandle.WaitAll in an STAThread ?

P: n/a
[the xpost is justifiable, I think]

Just now I translated Jon Skeet's WaitHandle.WaitAny / .WaitAll
(<http://yoda.arachsys.com/csharp/threads/waithandles.shtml>) from C#
into VB.NET, and after the first go I ran into trouble:

(from the docs for WaitHandle.WaitAll)
NotSupportedException The number of objects in waitHandles is greater
than the system permits.
-or-

The current thread is marked with the STAThreadAttribute, and
waitHandles contains more than one element.


After digging a bit, I came to the conclusion that VB.NET apps, even
ones that aren't explicitly marked for COM Interop, are STA threaded by
default, so a simple application of the MTAThread attribute to my Main
method fixed the problem.

However, what would I do if I had a real app that *needed* to be
STAThread'd, and I wanted to wait for multiple signals? The docs offer
no justification, just a blunt

Note The WaitAll method is not supported on threads that are marked
with STAThreadAttribute.

Why not? And what could one do about it?

--
Larry Lard
Replies to group please

Feb 16 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
| [the xpost is justifiable, I think]
|
| Just now I translated Jon Skeet's WaitHandle.WaitAny / .WaitAll
| (<http://yoda.arachsys.com/csharp/threads/waithandles.shtml>) from C#
| into VB.NET, and after the first go I ran into trouble:
|
| (from the docs for WaitHandle.WaitAll)
| >>
| NotSupportedException The number of objects in waitHandles is greater
| than the system permits.
| -or-
|
| The current thread is marked with the STAThreadAttribute, and
| waitHandles contains more than one element.
| >>
|
| After digging a bit, I came to the conclusion that VB.NET apps, even
| ones that aren't explicitly marked for COM Interop, are STA threaded by
| default, so a simple application of the MTAThread attribute to my Main
| method fixed the problem.
|
| However, what would I do if I had a real app that *needed* to be
| STAThread'd, and I wanted to wait for multiple signals? The docs offer
| no justification, just a blunt
|
| Note The WaitAll method is not supported on threads that are marked
| with STAThreadAttribute.
|
| Why not? And what could one do about it?
|
| --
| Larry Lard
| Replies to group please
|

The reason for this is that "WaitAll" boils down to a call to
"MsgWaitFormultipleObjects" Win32 API with bWaitAll set to TRUE. This would
deadlock when it was allowed to be called from a thread that creates windows
(like an STA thread does). Please check msdn to get more info on the
semantics of MsgWaitFormultipleObjects, especially the remarks are
enlightening.

Willy.


Feb 16 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.