Re: Service hangs when accessing ActiveX control



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).


.



Relevant Pages

  • Re: Service hangs when accessing ActiveX control
    ... >> in an MTA by default, if this is not what you want, you have to create ... >> own STA thread. ... > Not specifying any attribute or specifying it as STA results in the same ... > I wouldn't make it a control, but a simple class that exposes the same ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Running GUI application in separate application domain
    ... >I don't think remoting is the issue here and I believe the remoting ... > the assembly in the new appdomain I now set the apartment state to STA. ... host COM based controls or a .NET control that wraps a COM AX ... The performance counters indicate that there are a ton ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Missing events
    ... but this thread doesn't run a message pump. ... implied because of the 3rd party VB COM control I use? ... and then initialises the hardware (camera, lens ... class as the original lens object I had. ...
    (microsoft.public.vc.atl)
  • Re: Thread-safety in VB+MFC Nested ActiveX controls
    ... It makes no sense whatsoever to host an ActiveX Control ... The container must use STA and must ... MTA and windowing do not mix well ...
    (microsoft.public.win32.programmer.ole)
  • Re: Thread-safety in VB+MFC Nested ActiveX controls
    ... Thank you Alexander, But as I mentioend, that control IS hosted in STA using ... COINIT_APARTMENTTHREADED, and ActiveX control is also Apartment Threaded, why ... Mine is MTA client. ...
    (microsoft.public.win32.programmer.ole)