Rob,
> This threading problem has been beaten to death:
Maybe so, but 90% of the people just put <STAThread> _ in front of main
and it solves there problem. As I said, if I use the files created by
TLBImp.exe I can dynamically create the controls just fine, however
only 1 event will work. Attempting to add more event handlers to the
control causes the error I mentioned. No one seems to be able to say
in code how to make this work.
If I add the reference to the AXImp.exe created file, then that wrapper
gives me all the events, but it won't work in a MTA. So I either need
to figure out how to get the first option working by just getting
access to the other events somehow, or I need to put it into a thread.
However, all of the examples I can find, show a very simple process in
1 subroutine that they spawn into a thread. I need to run and entire
APPLICATION in a thread if I am going to try this method.
> - you cannot create the COM object from one of
> the OnXyz methods unless you consume the object in
[quoted text clipped - 7 lines]
>
> from within the OnXyz method, of course.
Ok, so where does this code go? Can it go in the OnStart Method? Or
do I have OnStart call my own procedure? In any case I have tried two
other ways of making the thread STA and can't figure out how to do it.
Where does the code go?
OnStart()
Thread.CurrentThread.ApartmentState = ApartmentState.STA ?
I had:
<STAThread> _
OnStart
I also tried to put that code in Main and everywhere else I could think
of, it did't work, I get the same threading error.
As for trying to create things in a new thread, please forgive in
advance my ignorance on threading. But I have multiple controls that
have events, properties and methods. So I instantiate the main control
and set it's properties. I execute one of it's methods to connect to a
device. When it connects, the connected event fires. There is code in
the connected event that sends data over the line. If a disconnect
occurs for any reason, the disconnected event fires. Where does all
this code go? Does just creating the component in a thread
automatically make all it's event handlers and code operate in that
thread?
Protected Overrides Sub OnStart(ByVal args() As String)
Dim t As Thread
t = New Thread(AddressOf Me.myProcess)
t.Start()
end sub
Private Sub myProcess()
myOCX = New AxmyObject.myOCX
myOCX.Enabled = True
myOCX.ConnectToDevice()
end sub
Private Sub Connected()
myOCX.SendData()
... lots more code
end sub
Private Sub Disconnected()
... do some more stuff
end sub
Fred
Willy Denoyette [MVP] - 12 Nov 2005 22:31 GMT
1. STAThread attribute is not needed on Main in a Windows Service, you
service code should run in a thread spawned from OnStart, this thread needs
to be initialized for STA before starting the thread:
t.ApartmentState = ApartmentState.STA
t.Start()
2. You should not set a reference to the AXImp.exe generated stuff, this is
only needed when you want to host the control in a control container (like a
Windows Form), that container needs to run in an STA thread (all AX controls
need an STA thread to run on).
3. When you want to use an AX control from a Windows Service, you need to
set a reference to the tlbimp.exe generated Interop assembly, but keep in
mind that hosting AX controls (especially Full Controls are problematic) in
Windows Services is not a supported scenario, so use it at your own risk.
Willy.
> Rob,
>
[quoted text clipped - 76 lines]
>
> Fred
fdeckerNOSPAMM@aol.com - 14 Nov 2005 19:34 GMT
I really appreciate the help. I think I am getting close. Ok, here is
my dilemna, there is a difference in the way .NET handles events and
all else before it. I believe that .NET must have an event sink for
each event and normal ActiveX implementations such as VB.NET have only
one event sink, the form. Somehow it allows multiple events to fire
using one sink. I believe the AxImport wrapper must simulate this and
translates the one sink to multiple sinks. This is my first of two
problems.
1. If I try to reference the component without using Ax as in "Dim
withevents myOCX.myControl", no matter where I put it or how I use it,
I get the 0x80040202 error as soon as I try to put more than one event
handler in the code for that component. The program will work
perfectly right out of Start() if I want it to if I could live with
only one event handler.
2. So I therefore try to Dim WithEvents AxmyOCX.AxmyControl. Now I use
the Ax wrapper. Now I get no errors, but I also get no events. :) So
I try to create a form too and put the controls on a form. I must be
missing some key piece like loading the form or starting a message
pump. I also have to use the
((System.ComponentModel.ISupportInitialize)(myOCX)).BeginInit() and
EndInit commands or I get an error. Here is my code, it must be a mess
by now I've tried so many different things:
Private WithEvents myOCX As AxMyControls.AxMyOCX
Private Sub OnStart()
Dim t As Thread
t = New Thread(AddressOf Me.myStart)
t.ApartmentState = ApartmentState.STA
t.Start()
End Sub
Private Sub myStart()
Try
myForm = New System.Windows.Forms.Form
System.Windows.Forms.Application.Run(myForm)
myOCX = New AxMyControls.AxMyOCX
CType(myOCX,
System.ComponentModel.ISupportInitialize).BeginInit()
myForm.Controls.Add(myOCX)
CType(myOCX,
System.ComponentModel.ISupportInitialize).EndInit()
AddHandler myOCX.OnEvent1, AddressOf myOCX_OnEvent2
AddHandler myOCX.OnEvent2, AddressOf myOCX_OnEvent2
myOCX.Enabled = True
Catch e As Exception
' error code here
End Try
Thanks... Fred