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

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 'GUID' because the current thread is not in a single-threaded apartment.
Note GUID 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 "Symptoms" 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:
  1. In the code for your host application, locate the following code:
    <MTAThread()> _
  2. Replace the code that you located in the previous step with the following code:
    <STAThread()> _
  3. Build your host application, and then run your host application.

    The problem that is mentioned in the "Symptoms" 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:
  1. If you start a thread that is named MyThread in the code for your host application, locate the following code:
    MyThread.Start()
  2. 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
  3. Build your host application, and then run your host application.

    The problem that is mentioned in the "Symptoms" section does not occur.
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:

  1. 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
  2. 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
  3. Build your host application, and then run your host application.

    The problem that is mentioned in the "Symptoms" section does not occur.

Status

This behavior is by design.

More Information

Steps to reproduce the problem

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. Use Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 to create a Windows Application project.

    By default, the Form1 Windows form is created.
  3. Add any ActiveX control, such as the Microsoft Web Browser control, to the Form1 Windows form.
  4. If you want to specify that the main thread in the host application runs in an MTA, follow these steps:
    1. In the Form1.vb file, locate the following code:
      End Class
    2. 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
    If you want to start a new thread without specifying the apartment state of the thread, follow these steps:
    1. In the Form1.vb file, locate the following code:
      End Class
    2. 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
    If you want to specify the apartment state of a new thread as multithreaded before the thread is started, follow these steps:
    1. In the Form1.vb file, locate the following code:
      End Class
    2. 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
  5. Build your host application, and then run your host application.

    The problem that is mentioned in the "Symptoms" section may occur.

References

For additional information, visit the following Microsoft Web sites:

ThreadStateException classApplication.Run method (Form)Form classActiveX controlsProcesses, threads, and apartmentsThread class For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
316422 Roadmap for threading in Visual Basic .NET

Propriedades

ID do Artigo: 841295 - Última Revisão: 16 de nov de 2007 - Revisão: 1

Comentários