Saturday, April 14, 2007 

More .NET / STA madness...

I was just writing a piece of code to coordinate two cooperating processes under Windows, and that involved signaling a kernel object and waiting on another.
Now, the Win32 API has a nice function to do just that: SignalObjectAndWait. I was writing the code in C#, but hey, the WaitHandle happens to have a similar function (SignalAndWait). A WaitHandle isn't much more than a wrapper over kernel objects, so that wasn't really surprising: that function ought to be just a wrapper over the API.
What was surprising was the exception I got trying to use it: apparently, you cannot call that function from an STA thread. By the way, my thread happened to be in an STA just because of code Visual Studio itself had generated. I'm not using COM and I shouldn't be bothered with this stuff.
Again, COM is raising its ugly head behind all the .NET stuff. Again, some framework implementer thought it was wise to protect some unaware user from getting his message pump stuck. Which is kind of ridiculous, as you can just as easily get your message pump stuck by decomposing the forbidden call in a signal and a wait. Oh well...

Labels: ,