0

I have read little bit about COM threading model: MTA and STA, for example this SO post:

Difference between MTA and STA

There we can read that to communicate with object within STA we need to pump messages to this apartament.

My question is how to do it (C#/VB.NET)? How can I pump message from another thread, so for example, text box in WinForms updates (chagnes its text or whatever)?

Any remarks/pointers are acceptable too.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • You *could* use Application.DoEvents, but that can be trickier than it looks: https://stackoverflow.com/questions/5181777/use-of-application-doevents and https://blogs.msdn.microsoft.com/jfoscoding/2005/08/06/keeping-your-ui-responsive-and-the-dangers-of-application-doevents/ the real question is why do you need to pump messages? – Simon Mourier Sep 13 '18 at 17:41
  • 1
    You misunderstood the post. It is the responsibility of the thread that joined STA to pump messages sent to its windows; if it doesn't, COM objects it hosts won't work. COM calls to STA objects from outside their apartment are implemented by sending window messages to a special hidden window maintained by COM runtime; if the thread doesn't pump messages, those calls won't get delivered. You can't force the thread to pump messages from outside - it just something it does (or does not, in which case it's buggy). Now, is there a problem you are trying to solve? – Igor Tandetnik Sep 13 '18 at 18:05
  • @IgorTandetnik Just pure curosity I guess. I just have been reading about platform invoke and COM interop and wanted to have full view of how things work. – Michał Turczyn Sep 13 '18 at 18:12
  • [This article](https://support.microsoft.com/en-us/help/150777/info-descriptions-and-workings-of-ole-threading-models) is the best I know of on the subject. It doesn't describe interop though, just "classic" COM. – Igor Tandetnik Sep 13 '18 at 18:17
  • Thanks @IgorTandetnik, you could put your comments together in an answer, you helped a lot :) – Michał Turczyn Sep 13 '18 at 18:22

0 Answers0