Can't WaitHandle.WaitAll in an STAThread ?

L

Larry Lard

[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?
 
W

Willy Denoyette [MVP]

| [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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top