hi, i m developing a windows forms application using MC++, when i do
debug the project , it fails in the line of '
openFileDialog1->ShowDialog(); ' and gives an error like that :
An unhandled exception of type 'System.Threading.ThreadStateException' occurred in System.Windows.Forms.dll
Additional information: Current thread must be set to single thread
apartment (STA) mode before OLE calls can be made. Ensure that your
Main function has STAThreadAttribute marked on it. This exception is
only raised if a debugger is attached to the process.
But its already done in Form1.cpp like that :
System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA;
i mean the current apartmentstate is already set as STA... i cant debug the project cos of that error...

STAThreadAttribute problem in managed c++ windows forms
balazs HIDEGHETY
dsmerchek
Kkirtac,
I'm having a similar problem with my program too. If I do a "Ctrl+C" on a datagrid, i get that error.
I actually did this before my Application::Run();
MessageBox::Show(__box(System::Threading::Thread::CurrentThread->ApartmentState)->ToString());
And I the messagebox popped up with: "MTA" even though I had it set to STA
See what happens when you popup that message box.
By the way...googling this problem led me nowhere.
wollka
Have you put the MTAThreadAttribute on you main function e.g.:
[MTAThreadAttribute]
int main(array<System::String ^> ^args)
//...
Wong Tin Chi Timothy
Hi,
I do not have any problem with that. Can you provide more code
Max
Carlo Razzeto
bozworthy
Ramino
Hmm, it works just fine in VS 2005, in the VS 2003 it takes a lots of time to get in to debugger, but otherwise works fine and produces STA message.
Max
Thorsten Ueberschaer
I got a similar error myself and it took me ages to get around it.
It turns out that the line
System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA;
isn't good enough.
make sure that the main function has the STAThreadAttribute. That should fix it for you.
[STAThreadAttribute]
int main(array<System::String ^> ^args)
I'm not actually sure why this works. I thought that the STAThreadSttribute just set the ApartmentState property anyway.
Anyone have any idea
HOTBOT
RobHall
Helinho