Help with Threading

G

Guest

VS 2005 / C++ / __gc program (converted from VS 2003)

i have this code in my program:

int found;
String *temp;
this->ofdOpen->ShowDialog();

the last line produces the folloing unhandleld exeption:


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.


However my Main funtion lookes like this:

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR
lpCmdLine, int nCmdShow)
{
System::Threading::Thread::CurrentThread->ApartmentState =
System::Threading::ApartmentState::STA;
Application::Run(new fclMain());
return 0;
}

Can anybody explain to me whats wrong?
 
C

Carl Daniel [VC++ MVP]

cnickl said:
VS 2005 / C++ / __gc program (converted from VS 2003)

i have this code in my program:

int found;
String *temp;
this->ofdOpen->ShowDialog();

the last line produces the folloing unhandleld exeption:


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.


However my Main funtion lookes like this:

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
System::Threading::Thread::CurrentThread->ApartmentState =
System::Threading::ApartmentState::STA;
Application::Run(new fclMain());
return 0;
}

Can anybody explain to me whats wrong?

Was the call to ofdOpen->ShowDialog from your main thread, or from another?

-cd
 
W

Willy Denoyette [MVP]

System::Threading::Thread::CurrentThread->ApartmentState =
System::Threading::ApartmentState::STA;

makes no sense here, it's too late to set the ApartmentState at this point,
note also that _tWinMain is also not the real entry point of this 'WinForms'
application, so it makes no sense either to set the STAThreadAttribute as
suggested by the error message.
All you can do is set the /CLRTHREADATTRIBUTE:STA linker switch.


Willy.
 
G

Guest

I just realized that the entire structure of code is different when you
convert a VS 2003 solution to VS 2005 compared to starting a new project in
VS 2005. VERY frustrating. I decided to start the project over in VS 2005.
Thanks anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top