Microsoft KB Archive/841295

= You receive a System.Threading.ThreadStateException exception when you try to create an instance of a Windows form =

Article ID: 841295

Article Last Modified on 11/16/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-





SYMPTOMS
When you try to create an instance of a Microsoft Windows form that contains an ActiveX control, you may receive the following error message:

An unhandled exception of type 'System.Threading.ThreadStateException' occurred in system.windows.forms.dll

Additional information: Could not instantiate ActiveX control ' ' because the current thread is not in a single-threaded apartment.

Note  is a placeholder for the GUID of the ActiveX control.

Note This problem may not occur after the first time that you run your application.



CAUSE
The ActiveX control requires that the instantiating thread must be in a single-threaded apartment (STA). The instantiating thread is the thread that tries to create an instance of the Windows form. However, the instantiating thread is in a multithreaded apartment (MTA). This leads to a race condition that causes the problem that is mentioned in the &quot;Symptoms&quot; section.

The instantiating thread can be in an MTA if one of the following conditions is true:
 * In the host application, you use the MTAThread attribute to specify that the main thread runs in an MTA.
 * In the host application, you start a new thread without specifying the apartment state of the thread. In this case, the thread runs in an MTA.
 * In the host application, you specify the apartment state of a new thread as multithreaded before the thread is started.



WORKAROUND
To work around this problem, make sure that the instantiating thread runs in an STA.

If you use the MTAThread attribute to specify that the main thread of the host application runs in an MTA, you must use the STAThread attribute instead. To do this, follow these steps:   In the code for your host application, locate the following code:  _   Replace the code that you located in the previous step with the following code:  _  Build your host application, and then run your host application.

The problem that is mentioned in the &quot;Symptoms&quot; section does not occur.

If you start a new thread without specifying the apartment state of the thread, you must specify the apartment state of the thread as single-threaded. To do this, follow these steps:   If you start a thread that is named MyThread in the code for your host application, locate the following code: MyThread.Start </li>  Add the following code before the code that you located in the previous step: ' Specify that the MyThread thread runs in an STA. MyThread.ApartmentState = Threading.ApartmentState.STA </li> Build your host application, and then run your host application.

The problem that is mentioned in the &quot;Symptoms&quot; section does not occur.</li></ol>

If you specify the apartment state of a new thread as multithreaded before the thread is started, you must specify the apartment state of the thread as single-threaded instead. To do this, follow these steps:   If you specify the apartment state of the MyThread thread as multithreaded in the code for your host application, locate the following code: MyThread.ApartmentState = Threading.ApartmentState.MTA </li>  Replace the code that you located in the previous step with the following code: ' Specify that the MyThread thread runs in an STA. MyThread.ApartmentState = Threading.ApartmentState.STA </li> Build your host application, and then run your host application.

The problem that is mentioned in the &quot;Symptoms&quot; section does not occur.</li></ol>

<div class="status_section">

STATUS
This behavior is by design.

<div class="moreinformation_section">

Steps to reproduce the problem
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.</li> Use Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 to create a Windows Application project.

By default, the Form1 Windows form is created.</li> Add any ActiveX control, such as the Microsoft Web Browser control, to the Form1 Windows form.</li> If you want to specify that the main thread in the host application runs in an MTA, follow these steps:   In the Form1.vb file, locate the following code: End Class </li>  Add the following code before the code that you located in the previous step: <MTAThread> _ Public Shared Sub Main ' Run a standard application message loop on the current thread. Application.Run(New Form1) End Sub </li></ol>

If you want to start a new thread without specifying the apartment state of the thread, follow these steps:   In the Form1.vb file, locate the following code: End Class </li> <li> Add the following code before the code that you located in the previous step: Public Shared Sub Main ' Create a thread. Then run the LaunchForm method on the thread. ' This automatically runs the main thread in the application in an MTA. Dim MyThread As System.Threading.Thread MyThread = New System.Threading.Thread(AddressOf LaunchForm) MyThread.Start End Sub

Public Shared Sub LaunchForm ' Run a standard application message loop on the current thread. Application.Run(New Form1) End Sub </li></ol>

If you want to specify the apartment state of a new thread as multithreaded before the thread is started, follow these steps: <ol style="list-style-type: lower-alpha;"> <li> In the Form1.vb file, locate the following code: End Class </li> <li> Add the following code before the code that you located in the previous step: Public Shared Sub Main ' Create a thread. Then run the LaunchForm method on the thread. Dim MyThread As System.Threading.Thread MyThread = New System.Threading.Thread(AddressOf LaunchForm) ' Specify that the MyThread thread runs in an MTA. MyThread.ApartmentState = Threading.ApartmentState.MTA MyThread.Start End Sub

Public Shared Sub LaunchForm ' Run a standard application message loop on the current thread. Application.Run(New Form1) End Sub </li></ol> </li> <li>Build your host application, and then run your host application.

The problem that is mentioned in the &quot;Symptoms&quot; section may occur.</li></ol>

<div class="references_section">