On Jan 30, 3:49 am, "Pooh" <hiank...@gmail .comwrote:
hi all,
i faced an error in my project:
"An unhandled exception of type
'System.Threadi ng.ThreadStateE xception'
occurred in system.windows. forms.dll
Additional information: Could not instantiate ActiveX control
'648a5600-2c6e-101b-82b6-000000000014' because the current thread is
not in
a single-threaded apartment."
after i set as below:
Thread.CurrentT hread.Apartment State = ApartmentState. STA
other error occured:
Fail to set ApartmentState
i already put
<STAThread()_
PublicSub Main()
for more detail about my error:
<STAThread()_
PublicSub Main()
....
Thread.CurrentT hread.SetApartm entState(Apartm entSt ate.STA) -error
(fail to set.....)
InitializeCompo nent()
....
PrivateSub InitializeCompo nent()
...
Me.ScanSerial = New AxMSCommLib.AxM SComm
...
anyone know why?
please help...Thanks
Hmmm, it looks like your trying to use the old ms com control. Are
you trying to do some serial communication of some type? The reason I
ask is that as of .NET 2.0, this ability is built into the framework.
Look at System.IO.Ports .SerialPort.
Anyway, as to your current problem - you can't set the apartment state
of a running thread. So, you can't call
Thread.CurrentT hread.SetApartm entState(Apartm entSt ate.STA)
On your main thread - it is by definition already started. Setting
the STAThread attribute is how you set the main threads threading
model. You can confirm this - just check the current threads
apartmentstate (GetApartmentSt ate).
Anyway - i would look at using the built in serial stuff. I've heard
of people having issues with the COM control :) If you must use it,
then we might need to do a little more in depth analysis as to why
your getting the apartmentstate error.
--
Tom Shelton