Interestingly, the docs for STAThreadAttribute say:
<quote>
The COM threading model can be set to single-threaded apartment or
multithreaded apartment. The application thread is only initialized for
COM interop if the thread actually makes a call to a COM component. If
COM interop is not used, then the thread is not initialized.
</quote>
(I hadn't seen that before this post, btw.)
I *hope* it's accurate, but you never know...
Sure, I'd hope it's accurate too. But I've seen too many inaccurate
statements in the docs to be confident that it is.
Especially since when I compared two different console applications, the
only difference between the two being the presence of the STAThread
attribute in one, and saw that the one with the STAThread attribute does
in fact load additional DLLs, I become somewhat more confident that I
can't trust MSDN on this one.
I don't have a good explanation for why the particular DLLs that got
loaded did in fact get loaded. But I know for sure that they did, and I
know for sure that it's a stable behavior (that is, add the attribute,
they show up, remove the attribute, they go away).
Even if the behavior was different on someone else's computer, it's clear
to me that that attribute is doing more, even when COM isn't explicitly
involved, than just setting some flag in the managed thread for the COM
interop to look at if it ever gets invoked.
Something is _executing_ differently, even when COM isn't used (or else it
turns out that there's no such thing as a .NET console application that
doesn't use COM...all I know is the console application was a trivial
"print this float" program, and for all I know .NET _is_ in fact using COM
for some reason, but if so that's even more reason to be concerned, since
it means COM gets used implicitly for the most trivial programs).
Yes, that's true. Just to be clear though, I think we both agree that
it's not going to hurt garbage collection when COM *isn't* involved.
Well, I do agree with that statement. The problem is, though, that I
can't qualify "involved" with "explicitly". My experiment shows me that
something different happens with that attribute. It _might_ be that COM
gets used implicitly, in which case there could still be a garbage
collection issue. Or it could just be that .NET is loading extra DLLs
without actually using COM past that point, in which case I think it's
likely that wouldn't cause the GC issue to show up.
But I don't have any practical way to know the difference. I mean, if I
really cared I suppose I could research this and track down the actual
answer. But I don't, especially since I think there's already other good
reasons to leave off the attribute unless it's actually needed. So I just
put that data in the "one more reason not to include the attribute"
column.
Pete