C # .NET: "Multi Threaded Apartment" and "Multithreading"

I study the concepts of multithreading (in general, and are focused on C # .NET). Reading various articles, until now it has not been possible to fully understand several basic concepts.

I am posting this question. Hans Passant explained it well, but I could not understand part of it. So I started searching the Internet.

I am reading this question which has no answers.

Are multithreading and MTA the same?

Suppose I am writing a WinForm application that is an STA (as mentioned above by its Main () method), but I can create multiple threads in my application. I can say with confidence that my application is multithreaded. Does this also mean that my application is an MTA?

Speaking of STA / MTA, most articles (like this ) talk about COM / DCOM / Automation / ActiveX. Does this mean that DotNet has nothing to do with STA / MTA?

+5
source share
2 answers

Not. MTA is a single thread property, like an STA. Now you will make the very opposite promise: you declare that the thread does nothing to save external code without threads. Therefore, there is no need to have a dispatcher, and you can block as much as you want.

This has consequences, of course, and they can be quite unpleasant. This is deadly if the user interface thread of your program is in the MTA, because it uses so many external components that are fundamentally unsafe. The clipboard does not work, drag + drop does not work, OpenFileDialog usually just hangs with your program, WebBrowser will not fire its events.

Some components check this and throw an exception, but this check is not performed sequentially. WPF is noteworthy, while apartment status usually only matters for unmanaged code, WPF borrowed the concept and raises "the calling thread must be STA because it requires many user interface components." Which is a bit misleading, in fact it means that the thread must have a dispatcher for its controls to work. But otherwise, this is consistent with the promise of the STA.

It can work when the component uses COM, and the author has provided a proxy. Now the COM infrastructure is working to make the component thread safe, it creates a new thread, which is the STA, to provide it with a safe home. And each method call is automatically marshaled, so it works on this thread, thereby ensuring thread safety. The exact equivalent of Dispatcher.Invoke (), but is fully automatic. The consequence of this is that it is slow, easy access to resources, which usually takes several nanoseconds, can now take several microseconds.

You are lucky if the component supports MTA as well as STA. This is not common, only someone like Microsoft goes beyond a thousand miles to keep libraries streaming.

I should perhaps emphasize that apartment concepts are completely absent in the .NET Framework. In addition to the basics of formulating an apartment type, it is necessary, since .NET programs often need to interact with unmanaged code. Therefore, writing a Winforms application with workflows is very good, and these workflows are always in the MTA, however, you yourself are involved in thread protection and nothing is automatic.

This, as a rule, is well understood, because everyone knows how to use the lock keyword, task and background classes and knows that the Control.Begin / Invoke () method is required to update the user interface from the workflow. With InvalidOperationException to remind you when you make a mistake. Leaving it to the programmer instead of a system that cares about thread safety makes it difficult to use threads. But it gives you many opportunities to do this better than the system. What was needed, this system ensured thread safety by getting a serious black eye when Java hit it in the face during the intermediate wars of the late 90s.

+6
source

There are a few questions, but start with this first:

An apartment is the context in which a COM object is initialized and executed, and it can be either a single thread (STA), commonly used for unsafe objects, or for multiple threads.

the term "apartment", which describes the structures in which COM objects are created

From: https://msdn.microsoft.com/en-us/library/ms809971.aspx

Thus, multithreading and MTA are not the same, but MTA is multithreaded. You can say that STA and MTA are associated with COM objects . You can read more here: https://msdn.microsoft.com/en-us/library/ms693344(v=vs.85).aspx

So, for your second question, if your WinForm application is "multithreaded", this does not mean that it is "MTA".

Finally, the concepts of MTA / STA are older than .NET technologies, but we cannot say that they have nothing in common, because .NET supports COM technology in both STA and MTA.

I expect my answer to help you sort out the difference between apartment and threading.

More interesting here: Could you explain STA and MTA?

+1
source

All Articles