System.Threading.ThreadStateException

P

Pooh

hi all,
i faced an error in my project:
"An unhandled exception of type
'System.Threading.ThreadStateException'
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.CurrentThread.ApartmentState = 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.CurrentThread.SetApartmentState(ApartmentSt ate.STA) -> error
(fail to set.....)
InitializeComponent()
.....

PrivateSub InitializeComponent()
....
Me.ScanSerial = New AxMSCommLib.AxMSComm
....

anyone know why?
please help...Thanks
 
T

Tom Shelton

hi all,
i faced an error in my project:
"An unhandled exception of type
'System.Threading.ThreadStateException'
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.CurrentThread.ApartmentState = 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.CurrentThread.SetApartmentState(ApartmentSt ate.STA) -> error
(fail to set.....)
InitializeComponent()
....

PrivateSub InitializeComponent()
...
Me.ScanSerial = New AxMSCommLib.AxMSComm
...

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.CurrentThread.SetApartmentState(ApartmentSt 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 (GetApartmentState).

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top