By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,617 Members | 1,605 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,617 IT Pros & Developers. It's quick & easy.

COM Events from C# Service

P: n/a
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

Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Are you sure that the apartment threading model is STA?

I experienced a similar problem with a third party COM API that fired events
back to a worker thread it was created in. They said it was an STA apartment
but it turned out that it was actually MTA. Once I changed it I received all
the events as expected.

HTH

Ollie Riches

<an****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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

Nov 17 '05 #2

P: n/a
Thanks for the reply Ollie.

I did some further testing and I can confirm that the event is actually
firing on an MTA thread even though I actually create the COM object on
a thread that I mark as an STA thread!! Hmmmm...

Can you please elaborate on exactly what you changed in your
application to get the events working?

Cheers,
Ankur

Ollie Riches wrote:
Are you sure that the apartment threading model is STA?

I experienced a similar problem with a third party COM API that fired events
back to a worker thread it was created in. They said it was an STA apartment
but it turned out that it was actually MTA. Once I changed it I received all
the events as expected.

HTH

Ollie Riches

<an****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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


Nov 17 '05 #3

P: n/a
check out the property 'ApartmentState' on the Thread class.

http://msdn.microsoft.com/library/de...statetopic.asp
Ollie Riches
<an****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Thanks for the reply Ollie.

I did some further testing and I can confirm that the event is actually
firing on an MTA thread even though I actually create the COM object on
a thread that I mark as an STA thread!! Hmmmm...

Can you please elaborate on exactly what you changed in your
application to get the events working?

Cheers,
Ankur

Ollie Riches wrote:
Are you sure that the apartment threading model is STA?

I experienced a similar problem with a third party COM API that fired
events
back to a worker thread it was created in. They said it was an STA
apartment
but it turned out that it was actually MTA. Once I changed it I received
all
the events as expected.

HTH

Ollie Riches

<an****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
> 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
>

Nov 17 '05 #4

P: n/a
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.

<an****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Thanks for the reply Ollie.

I did some further testing and I can confirm that the event is actually
firing on an MTA thread even though I actually create the COM object on
a thread that I mark as an STA thread!! Hmmmm...

Can you please elaborate on exactly what you changed in your
application to get the events working?

Cheers,
Ankur

Ollie Riches wrote:
Are you sure that the apartment threading model is STA?

I experienced a similar problem with a third party COM API that fired
events
back to a worker thread it was created in. They said it was an STA
apartment
but it turned out that it was actually MTA. Once I changed it I received
all
the events as expected.

HTH

Ollie Riches

<an****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
> 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
>

Nov 17 '05 #5

P: n/a
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

Nov 17 '05 #6

P: n/a

<an****@gmail.com> wrote in message
news:11********************@f14g2000cwb.googlegrou ps.com...
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.
Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.