Threading

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

The preferred method of marshalling control back to the primary UI thread is
to call Control.BeginInvoke, but what if the primary user-interface class
doesn't derive from control, and hence doesn't have a BeginInvoke method
(i.e. it is an add-in, and as such derives from IDTExtensibility2, et al.)

Thanks
 
Patty O'Dors said:
The preferred method of marshalling control back to the primary UI thread is
to call Control.BeginInvoke, but what if the primary user-interface class
doesn't derive from control, and hence doesn't have a BeginInvoke method
(i.e. it is an add-in, and as such derives from IDTExtensibility2, et al.)

What do you mean by "primary user-interface class"? All you need is *a*
control which is executing in the thread you want to invoke in.
 
Hi Patty,

what you mean with the primary UI class?

What you need is to make sure that the method is executed in the target
thread, not in the thread that was executing. The only method I remember
right now to do such a thing is using Control.Invoke..
OF course the method may has no relation with the control at all, you only
use the control as an indication to where to execute the method. that's all.

Cheers,
 
Patty,

Assuming that your plug in is running on the main application's UI
thread (which is a reasonable assumption), you have a few options, I
believe. The first is a little hackish. I would create an instance of a
control on the UI thread (before you call out to your thread), something
such as a Panel. However, you would have to make sure that it is not
visible. It ^should^ be created on the same thread that you create it on.
Then, pass the Panel (or rather, its ISynchronizeInvoke implementation, this
is much cleaner) to the other thread, and call Invoke or BeginInvoke.

The other option you have is to expose the class with the methods you
want to call as a COM object. If you create the object with a threading
model of STA, then any calls made to it will be marshaled correctly across
the thread boundary, assuming that you have marshaled the interface pointer
correctly. In order to do this, you would have to get the global interface
table, and then register the interface (through a call to the
RegisterInterfaceInGlobal). This will return a cookie that you can pass
across the thread boundary, and then call GetInterfaceFromGlobal to
retrieve. There is a ton of interop invovled in this scenario, but I think
it is a more proper way of insuring your call is made on the correct thread.

Hope this helps.
 
Back
Top