System.Threading.ThreadStateException

ahpooh

Member
Joined
Jan 29, 2007
Messages
7
Programming Experience
Beginner
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

anyone know why?
please help...Thanks
 
ApartmentState must be set before starting the thread. ApartmentState property is obsolete in .Net 2.0, use SetAparmentState method instead.
See for example http://www.vbdotnetforums.com/showthread.php?t=13916

You probably want your main form in this state, for this you have to set STAThreadAttribute flag on Sub Main, but for Windows Forms applications in Visual Studio 2005 this method is *hidden* and inaccessible. Since STA threading is only relevant for COM objects this all must be a secret aganda to put down the COM model, I can't see it any other way ;)
 
thanks for reply...
but after i change to
Thread.CurrentThread.SetApartmentState(ApartmentState.STA)

the error also same:
fail to set specified COM apartment state


i already put
<STAThread()> _
Public Sub Main()

for more detail about my error:

<STAThread()> _
Public Sub Main()
....
Thread.CurrentThread.SetApartmentState(ApartmentState.STA) -> error (fail to set.....)
InitializeComponent()
....

Private Sub InitializeComponent()
...
Me.ScanSerial = New AxMSCommLib.AxMSComm
...


 
ApartmentState must be set before starting the thread.
I repeat: It is not allowed to change apartmentstate after a thread has started.
 
ya...i set it before thread start...

is it a project started will already be a MTA thread? because i haven't call thread function to start, but i already can get thread = MTA.
 
What thread is that?
 
em...
?Thread.CurrentThread
ApartmentState: MTA {1}
CurrentContext: {System.Runtime.Remoting.Contexts.Context}
CurrentCulture: {System.Globalization.CultureInfo}
CurrentPrincipal: {System.Security.Principal.GenericPrincipal}
CurrentThread: {System.Threading.Thread}
CurrentUICulture: {System.Globalization.CultureInfo}
ExecutionContext: {System.Threading.ExecutionContext}
IsAlive: True
IsBackground: False
IsThreadPoolThread: False
ManagedThreadId: 10
Name: Nothing
Priority: Normal {2}
ThreadState: Running {0}

is it any way to set default thread not to be MTA? Can set default to STA?
 
em...


is it any way to set default thread not to be MTA? Can set default to STA?
It can only be done by applying the STAThreadAttribute flag on Sub Main. Which can't be done in .Net 2.0 winforms app in VS2005 as I mentioned.
 
thanks for reply...

em...may i know why in this case only will occured when i debuging source code. but when i build the source code it wont occur and if i starting system by EXE it totaly no problem. Can you explain a bit for me? Thank you.
 
if

Program will throw exception when:
Me.ScanSerial = New AxMSCommLib.AxMSComm or
Me.wbrWorkInstruction = New AxSHDocVw.AxWebBrowser

is it not related with Thread? but why it show
Could not instantiate ActiveX control
'648a5600-2c6e-101b-82b6-000000000014' because the current thread is not in
a single-threaded apartment."


is it because MSCOMM32.OCX problem?
is it because MSCOMM32.OCX cant be use in vs2005?
 
Back
Top