Tuesday, October 27, 2009

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

Problem:- An unhandled exception of type 'System.Threading.ThreadStateException' occurred in System.Windows.Forms.dll

in microsoft visual c++

Solution:-

Just write '[STAThread]' above the main() function to resolve the problem. This code make your program single-threaded apartment (STA) instead of MTA.


Example:-

[STAThread]
int main(){

Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew UserForm());
return 0;
}

No comments: