Re: Service hangs when accessing ActiveX control



Stephen is right, the STA/MTA attribute is honoured on Main in a C# program,
but it's NOT true for a VB.NET program (as it's the case here if I'm not
mistaken) where the thread running Main is always entering an STA, sorry for
being incomplete.
The OnStart and other service control functions run on an MTA thread and
this cannot be changed, anyway you should never run service code in these
functions, you should start another thread initialize it to enter the
correct apartment and create the form and the COM object.

Note also that you should never run windows forms, nor should you create
instances of ActiveX controls in a windows service, it's inherently unsafe
and it won't work any longer in Vista, be warned!

Willy.


"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: 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: Service hangs when accessing ActiveX control
    ... correctly in an STA on the main thread but you are later calling into this ... your OnStart method and then create the form and control in the ThreadStart ... Application.Runto start the message pump. ... > Not specifying any attribute or specifying it as STA results in the same ...
    (microsoft.public.dotnet.framework.interop)
  • 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)
  • Privatization of Public resources rears its ugly head in Boston!
    ... (MTA), an independent agency, and the Governor's office. ... By the late 1990s, though, the project delays and cost ... In order to keep the project afloat, two key shifts in Big Dig ... The first was the transfer of control from ...
    (misc.news.internet.discuss)