COM Events from C# Service

A

ankurw

Hi All,

I am experiencing a frustrating issue with hooking into COM events from
a Windows service writting in C#.

I create a COM object in the service and hook into an event exposed by
the object. The event gets raised a few times (3 times on a Win2k box
and only once on a Win. 2003 box to be precise) and then simply stops.

My setup is:

1. I have a manager class (market internal) within my service.

2. The service OnStart creates an object of this class and calls the
Initialise method.

3. The Initialise method spawns a new thread (new Thread(new
ThreadStart(...)). I set the ApartmentState for this thread to STA and
start a message pump (Windows.Forms.Application.Run()) at the end of
the thread proc.

4. I instantiate the COM object on this thread and assign the reference
to a module level (static) variable defined in the manager class (ref.
#1)

I receive the event the first few events after the service starts and
then nothing. Not sure if this is some weird garbage collection issue
or what - I am completely stumped (and somewhat frustrated) right now.

Any help/pointers will be greatly appreciated.

Cheers,
Ankur
 
W

Willy Denoyette [MVP]

What kind of COM object is this? If it's a "Free" threaded object, it will
always be instantiated on the host MTA thread. Please check the COM object's
threading requirements using oleview or directly in the registry.


Willy.
 
A

ankurw

Thanks for your replies Willy and Ollie.

Ollie, in response to your post, I am in fact using the ApartmentState
property to mark the thread that I create as an STA thread.

Willy, I am not entirely sure what the threading model is for COM
object. The way the COM library that I am using has been structured is
that they have a factory object and I have to use this to instantiate
any other objects that I need [something like
TheFactoryObject.CreateObj("AnotherObj")]

There is no entry in the registry for the objects that I create via the
factory object. I couldn't even find the ThreadingModel entry for the
factory object. Using OleView on the factory object, there is a tab
called InProc Handler where the "Path to Implementation" is set to
Ole32.dll and the "ThreadingModel" is set to None.

Don't know where that leaves me!! :)

Cheers,
Ankur
 
W

Willy Denoyette [MVP]

Thanks for your replies Willy and Ollie.

Ollie, in response to your post, I am in fact using the ApartmentState
property to mark the thread that I create as an STA thread.

Willy, I am not entirely sure what the threading model is for COM
object. The way the COM library that I am using has been structured is
that they have a factory object and I have to use this to instantiate
any other objects that I need [something like
TheFactoryObject.CreateObj("AnotherObj")]

There is no entry in the registry for the objects that I create via the
factory object. I couldn't even find the ThreadingModel entry for the
factory object. Using OleView on the factory object, there is a tab
called InProc Handler where the "Path to Implementation" is set to
Ole32.dll and the "ThreadingModel" is set to None.

Don't know where that leaves me!! :)

Cheers,
Ankur

I see, you are trying to use OLE linking and embedding in a windows service,
this wont work, OLE is not designed for this environment, sorry.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top