Theme of the apartment

All,

I used streams for a while in C #, but I'm still a little confused about what the state of a stream apartment really means. I know that WinForms should always use the state of the apartment STA (as opposed to MTA), but I still do not understand what state of the apartment is all about.

+3
source share
2 answers

COM had very high goals. One of them was that the threads were programming parts that were very difficult to work properly, and they needed to be controlled using support libraries. Unlike .NET, where you still need to use classes that are not thread safe in thread safe mode.

, COM- ThreadingModel, , . "", " ". COM, , - . , COM , . , , - . , Control.Invoke Dispatcher.Invoke , .

, , . COM . , CoInitialize(), COM, COM-. , . , "". STA (Single Threaded Apartment) MTA (). STA - COM-, . MTA .

, . STA, STA. :

  • Windows

- , COM- . . , , , . .

, COM- MTA. , STA. COM , . COM STA, . , , , , .

COM , 98% , . , 2%, , , . , , , .NET .

COM , - Windows..NET Framework . , Thread.Join() Monitor.Enter() STA, COM, CLR- . - [STAThread], Main() GUI Thread.SetApartmentState(), CLR CoInitialize() . , , (, OpenFileDialog) STA. , , STA.

+8

" " STA MTA:

+ =

[., ] COM , GUI -GUI . , GUI, , , GUI GUI. ; WaitForSingleObject. , ?

, , , , . GUI , ! [., ]

+2

Source: https://habr.com/ru/post/1795754/


All Articles