Re: threads/marshalling/callbacks
Jason S <jmsachs@gmail.com> wrote:
Ah, I think I understand what I was missing. Is the following correct
then: If I create a COM STA ICallBackSink object in the same thread as
the one which is handling my application's window procedure, then COM
will utilize that same message queue to handle ICallBackSink's
methods.
Correct (assuming this thread has initialized STA, but it would be very
foolish for it to do otherwise).
If the window procedure is in the middle of something, then
whichever thread in the callback object which is calling Notify() will
block until the message queue is free.
The cross-apartment call into STA thread is implemented by posting a
message to the thread's message queue. Yes, the calling thread will
"block" (not exactly: if the calling thread is STA, it will spin a modal
message pump) until your thread gets around to retrieving this message
from the queue and dispatching it to a hidden window COM has created in
your thread. That window's window proc will then actually call Notify()
method. Once the method returns, window proc will marshal any results
back to the caller, at which point it will "unblock" and continue
execution.
The specific situation that's making me think of this, is that I have
an erratic series of real events that can occur repeatedly and very
quickly (with up to, say, 100usec repetition rate). I have a client
application that I want to notify only on the first new event that
occurs, so that it can check what events are new. I can't take the
time hit (several hundred microseconds) of an inter-apartment COM
call
Well, you more or less have the answer already. Your server needs two
threads: one to handle the stream of events, and the other to make
outgoing COM calls. The first thread does not need to be COM-aware;
communicate between the threads by some non-COM means (e.g. events or
window messages).
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925