M
Martijn Mulder
What is the penalty if I do not put [STAThread] just above the Main()
method?
method?
Martijn Mulder said:What is the penalty if I do not put [STAThread] just above the Main()
method?
Well, if you use COM components that rely on being on an STA thread,
they may well break. Many of the WinForms components fall into that
category.
If you're just writing a console app and not using any COM components,
you should be fine.
Michael Nemtsev said:MTA is appropriate for back-end, server side, where u work with COM/COM+
Michael Nemtsev said:Yep, but if u are working with UI-orient COM object u still need to appy
[STAThread] to get synchronization with UI controls.
Seems that Jon took this into account
Willy Denoyette said:| Nah - I hardly know anything about COM. I've seen various things about
| COM marshalling "just working" that never seemed to "just work" when I
| tried it. My knowledge is very flaky in this area. I want it to stay
| that way in some senses - I hope to have very little to so with it!
Well, you can hardly stay out of it, remember .NET on Windows is COM3 ,
take away COM and look what's left.
Also, don't believe that COM is going
away, it's just moving down from the user level to the system level, Vista
has a lot more COM components added than there are .NET components. Actually
Vista can run without .NET, but it can't without COM.
Note that I'm not talking about technologies built on top of COM (like
ActiveX, OLE etc), I'm talking about the low level COM binary interface
standard, which makes it possible for software components (whatever
language) to interoperate seamlessly using a well defined standard
interface.
Once you understand the COM interface definitions (the object model) and
it's rules, you'll better understand why/how things are working (or
fail).
Willy Denoyette said:| Quite a lot, IMO. Put it this way - I doubt that Mono on Linux uses
| COM, and it still manages to be useful. Java doesn't use COM (at least
| not on non-Windows platforms) and manages to achieve many of the same
| things as .NET. I don't see why COM is a necessary part of applications
| which don't appear to naturally require it.
True, but that's because Linux doesn't have COM. That means you don't need
to interop with existing COM servers (like the Office product suite,
Exchange server, a zillion of VB6 applications....).
Believe me, without COM, .NET would have died before it was even
born, for the same reason why Java failed on the Windows client.
Nor does it have to interop with things like Active Directory,ADO, COM+
services, CDO, OLEDB, OleTx, MSMG, DirectX and WMI to name a few, that's why
there are no System.DirectoryServices, System.EnterpriseServices,
System.Messaging, System.Data, System.Transactions, System.Drawing and
System.Management namespaces available in mono.
| Hmm... unfortunately, in my limited experience things don't quite work
| as they're meant to. For instance, the way you expressed the use of COM
| in a previous message suggested that if I'm using a COM wrapper that
| requires an STA, it's more efficient to specify STAThread but that
| things should still work without, via marshalling. Well, thinking of
| the one time I've definitely had to use COM explicitly in C#, things
| didn't work at all without STAThread. (I can't remember the exact error
| message, unfortunately.) It could very well be that this was a badly
| written COM library, but my impression from messages I've read over the
| years is that there are more badly written bits of COM than well
| written ones...
The major problem with .NET is the fact that users start to run multiple
thread to create all kind of objects, without respecting some basic rules.
Two important rules that seems to be forgotten quite often, are:
- Windows UI elements have thread affinity, so don't touch them from another
thread than the creator's thread, and
- 'Apartment' threaded COM objects have STA thread affinity, and while COM
takes care of this, it's far better to arrange that your objects run on the
creator's STA thread, which is preferably the UI thread. If you don't take
care of this, you'll incur marshaling overhead, worse, if your objects
marshals to the COM default thread, make sure it's a pumping thread (like
the UI thread), or you will block the finalizer thread.
Note that you might have some issues with 'apartment' threaded objects in a
multi-threaded application, but once you follow some simple rules, they are
quite simple to use. Note also that most COM classes are 'both' threaded, so
they can live in an STA and an MTA. Some quite sophisticated COM objects are
thread neutral (just like CLR objects), and they don't need an apartment to
live in, but they are quite complex to author (correctly).
Honestly, take some time to learn the basics of COM, I'm sure you will like
it.
Conclusion, if you need to create 'apartment' threaded objects, make sure
your thread is initialized as STA, if they are 'both' or 'free' threaded,
prefer an MTA for best performance.
If your COM ojects are 'single' threaded, don't use them in .NET, go back to
VB.
Willy said:| And that's where we seem to be a far cry from a comment from your
| original post:
| "but it works, that's the beauty of COM."
No it's not, it works, but you need to respect some simple rules of COM,
just like you need to respect many rules when using .NET components or Java
components whatever.
Yep, but it's not that difficult to remember the basic rules, if you care,
but I guess that's your problem, you don't care (or should I say don't like)
about COM.
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.