Re: Service hangs when accessing ActiveX control
- From: "Stephen Martin" <smartin@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 15 Sep 2005 10:12:28 -0400
While the explanations that you have received so far are incorrect the
solutions offered up are essentially correct.
When a service process starts it starts up exactly like any other process so
Main is called and the STA/MTA attribute is indeed honoured. I'm not sure
how it could be ignored since the process has no idea it is even a service
process until ServiceBase.Run is called. What does happen is that the main
thread of a service process is suspended by the Service Control Manager when
ServiceBase.Run is called and all further interaction (OnStart, OnPause,
etc.) is done on different threads. In your case you have created your form
correctly in an STA on the main thread but you are later calling into this
apartment from a different thread. The COM marshaller attempts to marshal
this call to the correct apartment but that involves sending a message to a
window owned by the original thread. Unfortunately, the main thread is
suspended and can never read the message and so your call hangs.
The correct procedure is, as has been suggested, to create an STA thread in
your OnStart method and then create the form and control in the ThreadStart
procedure of that thread. You must then call either form.ShowDialog or
Application.Run(form) to start the message pump. In your OnStop you can then
Close the form and allow the thread to exit.
There is no inherent reason not to use windows and message pumps in a
service. It is fairly rare to need to do so but it can be very useful
especially in cases such as using legacy components or for inter-thread
communications.
"Lucvdv" <replace_name@xxxxxxxx> wrote in message
news:ojmii11gihccqvna3f4ou9484skhdim7g2@xxxxxxxxxx
> On Wed, 14 Sep 2005 18:11:36 +0200, "Willy Denoyette [MVP]"
> <willy.denoyette@xxxxxxxxxx> wrote:
>
>> The STAThread attribute is ignored on Main in a service, Service threads
>> run
>> in an MTA by default, if this is not what you want, you have to create
>> your
>> own STA thread.
>
> But observation indicates the opposite.
>
> Not specifying any attribute or specifying it as STA results in the same
> behavior. Specifing it as MTA is different, in that it causes a
> ThreadStateException when the service is started - probably because the
> COM
> interface checks if it is running in STA.
>
> I can only conclude from that, that the attribute is not ignored in a
> service and the default is STAThread.
>
> VS.Net *does* explicitly put <MTAThread()> in the declaration (which I
> removed because I was using COM), and maybe it does so because it has to
> be
> that way for a service, but that's not the same as ignoring it.
>
>
>> > Just to see if there's a difference I changed it into <MTAThread>.
>> > That causes a ThreadStateException when the service is started, so it's
>> > not
>> > like the <STAThread> was ignored (which it would be if there was no COM
>> > interop).
>> >
>> Sure there is no difference because ... see above.
>>
>> >
>> > So I guess I'll have to port the control's code to a managed dll.
>> > I've already had to do that with another piece of code in another
>> > application, but this time it doesn't look as if it will be simple, and
>> > time is pressing :(
>>
>> Won't help you, Controls in .NET are UI elements and have the (almost)
>> same
>> requirements as AX controls.
>
> I wouldn't make it a control, but a simple class that exposes the same
> properties, methods and events (it has no UI elements, in that respect it
> behaves like the MS Winsock control among others).
.
- Follow-Ups:
- Re: Service hangs when accessing ActiveX control
- From: Lucvdv
- Re: Service hangs when accessing ActiveX control
- References:
- Service hangs when accessing ActiveX control
- From: Lucvdv
- Re: Service hangs when accessing ActiveX control
- From: Lucvdv
- Re: Service hangs when accessing ActiveX control
- From: Willy Denoyette [MVP]
- Re: Service hangs when accessing ActiveX control
- From: Lucvdv
- Re: Service hangs when accessing ActiveX control
- From: Willy Denoyette [MVP]
- Re: Service hangs when accessing ActiveX control
- From: Lucvdv
- Service hangs when accessing ActiveX control
- Prev by Date: Re: Service hangs when accessing ActiveX control
- Next by Date: Word/Excel check for an existing file lock
- Previous by thread: Re: Service hangs when accessing ActiveX control
- Next by thread: Re: Service hangs when accessing ActiveX control
- Index(es):
Relevant Pages
|