STAThreadAttribute problem in managed c++ windows forms

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...


Answer this question

STAThreadAttribute problem in managed c++ windows forms

  • misbrisco

    Have you put the MTAThreadAttribute on you main function e.g.:


    [MTAThreadAttribute]
    int main(array<System::String ^> ^args)
    //...

    This is not supported in a WinForms application and will cause the exception you've seen.



  • Mudasir

    its just an auto call...when i started the form application it was there..i really dont know what it is for..what you suggest

  • jstar

    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.


  • Ken Schall

    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



  • Shailesh Saini

    kkirtac wrote:
    its just an auto call...when i started the form application it was there
    I don't know what you mean by that. When I create a new WinForms C++ application there are no code setting the current thread's ApartmentState. Do you mean the previous author wrote that

  • Ben0s

    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



  • Steve Tyson

    thank you very much mate, i added [STAThreadAttribute] just above the main function and it solved the problem..thanks

  • Matt_chrs

    well i started coding this application in vstudio2003 and when i start a windows application, this code snippet was there automatically...and i installed vstudio2005 and its same when i start windows forms application...code is ok in "start without debugging" but when i debug it gives that error on the line i mentioned before...

  • mrodriguezc

    yes you are right njm137, so any solution exists no i didn't put that MTAThreadAttribute code on my main function....there exists STAThreadAttribute...but the problem is i can't debug the project..it stops on the line of OpenFileDialog1->ShowDialog( ) and gives that error..

  • KCTami

    Why, then, are you calling System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA;

  • rhyd

    Hi,

    I do not have any problem with that. Can you provide more code

    Max



  • STAThreadAttribute problem in managed c++ windows forms