How to pump message for COM STA threads in C #?

I have a main STA thread that calls many methods on a COM object and a secondary STA thread that works a lot on the same object. I want the main thread and the secondary thread to work in parallel (i.e. I expect interlaced output from the main and secondary). I know that I need to pump messages each time in the main thread - calling Get / Translate / DispatchMessage in C ++ will do the trick.

But I had a problem with working with the same strategy as in C #. First I used CurrentThread.Join () in the main thread to give control to the second thread. This did not work. Then I turned to Application.DoEvents () - I called it in the main thread whenever I needed a second thread to run. As a result, the second thread quickly grabs the control and does not release - the main thread cannot continue until the second thread is executed.

I am reading documents that say Application.DoEvents () will handle ALL pending events, and GetMessage () will only retrieve one message.

What is the right thing? Is there a C # equivalent for Get / Translate / DispatchMessage?

thank

UPDATE: The second thread is too fast, sending many COM message messages to the main STA thread. I just added delays in the second thread to slow it down. Now two streams basically work in parallel. But I would still like to know if there is a C # equivalent to GetMessage / TranslateMessage / DispatchMessage.

+5
source share
2 answers

++ STA. , , . COM-, , , , . STA , , . - , , .

- ++, , . COM STA COM- , . RPC_E_WRONG_THREAD.

Anyhoo, #. CLR . , , STA, . , 2- , . , CLR .

, , , 2- . concurrency, . . , , . . , COM- .

+7

Get/Translate/Dispatch .Net, Application.Run Dispatcher.Run() ( , winforms wpf) . , Get/Trans/Dispatch . , P/ Win32.

.Net , , hans ', , , COM, , DispatchMessage(), , (, , , ). , COM- .

+3

All Articles