Dragdrop and ThreadStateException

G

Guest

As a newcomer to C++ i .NET I've come across the following problem:
- When trying to establish drag&drop from one form to another I get a
runtime exception when the drop form is created:

(translated from swedish...)
System.InvalidOperationException: DragDrop-registration failed. --->
System.Threading.ThreadStateException: The current thread must have STA-state
(Single Thread Apartment) before OLE-calls can be made. Check that the main
function has the STAThreadAttribute declared.

As far as I can see this is already a fact:


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

I've tried to make the same statement in the form's constructor without
success.

I am using Visual Studio .NET 2003 .
Has anybody solved this?
 
R

raj

i think your problem is with creating another from the main sta thread.
you should use somethinng like this

to show or view a form use
ShowDialog function i.e. form.Show or form.ShowDialog()
instead of Application::Run use the Show or ShowDialog functions

this will create a new thread on which the new form will run. If you use the
main thread to create
a new form it will throw you exceptions.

I hope i am understanding this correctly. good luck

raj
 
G

Guest

Thanks for your answer but I think that's what I do.

From the main Form, form1, I create two other forms MealForm and
FoodDrinkForm as a response to clicking the in the main menu. The forms are
created and shown with code like this

private: System::Void menuItemMyMeals_Click(System::Object * sender,
System::EventArgs * e)
{

MealForm * mealForm= new MealForm( );
mealForm->Show( );

}

The
System.InvalidOperationException: DragDrop-registration failed. --->is thrown at mealForm->Show( );
This form contains handlers for DragEnter and DragDrop.

None of the forms participating in
the DragDrop-operation is a MDI-child of form1. Would that make any
difference? The application is an .exe because it contains some unmanaged
classes.

Any ideas?
-Örjan



--
Örjan Sterner
Mid Sweden University


raj said:
i think your problem is with creating another from the main sta thread.
you should use somethinng like this

to show or view a form use
ShowDialog function i.e. form.Show or form.ShowDialog()
instead of Application::Run use the Show or ShowDialog functions

this will create a new thread on which the new form will run. If you use the
main thread to create
a new form it will throw you exceptions.

I hope i am understanding this correctly. good luck

raj
 
G

Guest

Hi!

My problem is solved. The soloution was found on Adam Nathan's Win32 to
WinFX Blog July 18 2003. See
http://blogs.msdn.com/adam_nathan/archive/2003/07.aspx.

The solution defines another entry point.

Origninal code ( not working):

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

New code (working):

extern "C" int mainCRTStartup();

int main( ) { // Called by mainCRTStartup

Application::Run(new Form1());
}

// The true entry point set in linker advanced options
[STAThread]
int myMain()
{
// Initialize the CRT
mainCRTStartup();
return 0;
}


How come that I cannot find this solution or another workaround in
Microsoft's Knowledgebase or in some other official documentation?
I am a bad searcher or...?

-Örjan



--
Örjan Sterner
Mid Sweden University


raj said:
i think your problem is with creating another from the main sta thread.
you should use somethinng like this

to show or view a form use
ShowDialog function i.e. form.Show or form.ShowDialog()
instead of Application::Run use the Show or ShowDialog functions

this will create a new thread on which the new form will run. If you use the
main thread to create
a new form it will throw you exceptions.

I hope i am understanding this correctly. good luck

raj
 

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