You receive a System.Threading.ThreadStateException exception when you try to create an instance of a Windows form (841295)
The information in this article applies to:
- Microsoft Visual Basic 2005
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
SYMPTOMSWhen 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. CAUSEThe 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.
WORKAROUNDTo 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:
<MTAThread()> _ - Replace the code that you located in the previous step with
the following code:
<STAThread()> _ - 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:
- If you start
a thread that is named MyThread in the code for your host application, locate the following code:
MyThread.Start() - 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 - 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:
- 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 - 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 - 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.REFERENCESFor additional information, visit the following Microsoft Web sites: ThreadStateException class Application.Run method (Form) Form class ActiveX controls Processes, threads, and apartments Thread 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
Modification Type: | Minor | Last Reviewed: | 10/3/2006 |
---|
Keywords: | kbvs2005swept kbvs2005applies kbWindowsForms kbControl kbSample kbcode kberrmsg kbThread kbprb KB841295 kbAudDeveloper |
---|
|