OpenFileDialog^ dialog = gcnew OpenFileDialog;
dialog->ShowDialog();
What could possibly be wrong with this?
I’m using Visual Studio 2008 Professional on Windows 7 with .NET Framework 3.5
I created a button and added a Click Function and I put the above code, and just throw this error:
An unhandled exception of type 'System.Threading.ThreadStateException' occurred in System.Windows.Forms.dll
I translated it from Portuguese to English since my Visual Studio threw the error in portuguese.
Additional information: The current thread should be defined in STA
Mode (Single Thread Apartment ) before OLE calls can be done. Check if
the Main Function has STAThreadAttribute checked. The Exception will
only be generated if the debugger is attached to the thread (or maybe
process).
and this is the original error in Portuguese Language:
Additional information: O thread atual deve ser definido no modo STA
(single thread apartment) antes que chamadas OLE possam ser feitas.
Verifique se a função Main tem STAThreadAttribute marcado. A exceção
somente será gerada se um depurador estiver anexado ao processo.
By the way, all microsoft default stuff is set, all libraries that Windows Forms Application preset when creating a project,
I hope I was clear enough
Thanks in advance
It’s asking for you to put:
[STAThread]
on your main function. This means that clr should start a Single Threaded Apartment. Windows Forms wont work in a MTA (Multithreaded Apartment).
Further reading here: http://blogs.msdn.com/b/jfoscoding/archive/2005/04/07/406341.aspx