I am using a COM object from a third-party library that generates periodic events. When I use the library from the Winforms application, having the object as a member of the class and creating it in the main form stream, everything works. However, if I create an object from another thread, I do not receive any event.
I assume that I need some kind of event loop in the same thread that was used to create the object.
I need to use this object from a console application. I suppose I could use Application.DoEvents, but I would prefer not to include the Winforms namespace in the console application.
How can I solve this problem?
Update 3 (2011-06-15): The supplier finally responded. In short, they say that there is some difference between the message pump created by Application.Run and the one created by Thread.Join, but they do not know what the difference is.
I agree with them; any light shed on this question will be greatly appreciated.
Update:
From Richard's comment to mdm's answer:
if the other component is single-threaded and created from MTA, then Windows will create a workflow + window + message pump and perform the necessary sorting.
Trying to follow his advice, I do the following:
Update 2:
I changed the code after Joao Angelo's answer.
using System; namespace ConsoleApplication2 { class Program { [STAThread] static void Main(string[] args) { MyComObjectWrapper wrapper = new MyComObjectWrapper(); } } class MyComObjectWrapper { MyComObject m_Object; AutoResetEvent m_Event; public MyComObjectWrapper() { m_Event = new System.Threading.AutoResetEvent(false); System.Threading.Thread t = new System.Threading.Thread(() => CreateObject()); t.SetApartmentState (System.Threading.ApartmentState.STA); t.Start(); Wait(); } void ObjectEvt() {
I also tried the following:
public MyComObjectWrapper() { CreateObject(); }
c # events interop com
raven May 31 '11 at 9:32 a.m. 2011-05-31 09:32
source share