locked
Spawning a Form through a Thread RRS feed

  • Question

  • I have started a new Visual C++ project involving a class for a windows form. The class constructor invokes ShowDialog after the layout code. When creating the window using

    Code Snippet

    Window^ win1 = gcnew Window;


    my programs stalls. It does not move onto code after it hits that class construction. I know if I could run this form through a thread it would solve my problem but I have yet to figure out how to do that.

    Using
    Code Snippet

    _beginthreadex(0,0,&cbServer::initForm,win1,0,&ret1);


    ( initForm being a function that creates a Window object )

    throws errors because I can't pass win1 to it.

    Any suggestions on how I can get by this problem?
    Monday, January 28, 2008 11:13 PM

Answers

  • Do not call ShowDialog in Window's constructor. Instead try the following:

    System::Windows::Forms::Application::Run(gcnew Window);

    This will show Window modally in the proper context, with the application's message pump.

    Also, if you need threading inside of a managed context, use System::Threading::Thread instead of _beginthreadex.

    • Edited by ildjarn Friday, August 5, 2011 10:18 PM
    Tuesday, January 29, 2008 12:01 AM

All replies

  • Do not call ShowDialog in Window's constructor. Instead try the following:

    System::Windows::Forms::Application::Run(gcnew Window);

    This will show Window modally in the proper context, with the application's message pump.

    Also, if you need threading inside of a managed context, use System::Threading::Thread instead of _beginthreadex.

    • Edited by ildjarn Friday, August 5, 2011 10:18 PM
    Tuesday, January 29, 2008 12:01 AM
  • I get

    Code Snippet

    error C2061: syntax error : identifier 'Window'



    when I try using this.
    Tuesday, January 29, 2008 12:32 AM
  • The class name 'Window' is what you used in your first post; substitute that with whatever the actual name of your class is, and make sure you're including the header where that class is declared. You may need to fully qualify the class name if it's in a different namespace than where you're calling Application::Run.
    Tuesday, January 29, 2008 12:56 AM