The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by christophdotnet]

Alan,

I was wondering if could shed some light on this:

I am calling an COM component registered with ThreadingModel=Both from a C#
method that's invoked via an asynchronous delegate.

The COM component itself does some asynchronous processing once you called a
specific method and then you can sink events fired by the component (similar
to the asynchronous model of the Internet Explorer Automation Model). When I
am doing this within a Windows Forms app then I have to call
Application.DoEvents to spin a message pump on the thread that is executing my
method while I am waiting for the event from the component. Otherwise I will
not receive the event that tells me that the component finished processing.

That is nothing new so far. What surprised me though, is that when I do the
same thing in a Console application, i.e. call the COM component from a method
that's called via an asynchronous delegate then a simple Sleep is sufficient
to receive notifications from the COM component. The Main method of the
console app is marked with the [STAThread] attribute, but I did not think that
this attribute had any impact on the threading model of the ThreadPool
threads.

I would like to understand what's going on. I am wondering if for some reason
the ThreadPool thread in the WinForms app that is executing the my method was
initialized the run in the single threaded apartment and not uninitialized
before it executed my method. The ThreadPool thread in the Console App was not
initialized to run in the single threaded apartment, therefore the calls into
COM component are not synchronized and I can receive the event notifications
without a message pump.

Am I doing something wring or am I missing something. Do you have any
explanation about what's going on?

TIA,
Christoph Schittko
import-bot (20211) [Avatar] Offline
#2
Re: COM interop
[Originally posted by christophdotnet]

Alan,

one more thing I forgot:

System.Threading.Thread.CurrentThread.ApartmentState

returns

Unknown

in both cases.

Christoph
import-bot (20211) [Avatar] Offline
#3
Re: COM interop
[Originally posted by alan]

I'm sorry, I'm not an expert on COM interop. I can spend some time looking
into it, but it will take some time.
import-bot (20211) [Avatar] Offline
#4
Re: COM interop
[Originally posted by christophdotnet]

Not a problem I thought you may have came across an answer when you wrote your
book.

I'll do some more digging as well, since I would like to understand what's
going on.

In general, is calling Application.Start() and Application.DoEvents() the
recommended way to spin a Windows Message pump if you need one, even when you
are not in a Windows Forms App or are there better ways in the Framework to do
this?

Christoph