Launching a dialog from a secondary thread

I have a dialog based application that runs a function in a second thread.  Under certain conditions this function needs to launch a dialog.  I have created the dialog with a dialog template and it works fine from the primary thread, but not within the secondary thread.  It gives me an assert error when it runs the AfxGetThread() function on it in the MFC DoModal.  Is there any way to get it to work this way or do I need to dynamically create the window instead of using a dialog template?  (Or do I have a completely different problem causing the Assertion?)  Thanks,

David Johns
david_johnsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

inoxCommented:
Hi,
you should ensure your Dialog-constructor and DoModal call are in the same thread. try to construct the dialog by new CxxDialog in the threadfunction also (and don_t forget to delete the Dialogobject!)


0
SGyvesCommented:
Yes...the dialog needs to be created inside the thread. The reason for this is that anything derived from CObject can not be passed between threads. If you instantiate and DoModal inside of your thread...you will be okay. I had this same problem with my CSockets. Just hollar if you need more assistance.  :)

Stephen
0
PriyeshCommented:
This will work

//thread launching modal dialog.
UINT MyThread(LPVOID lParam)
{
   CAboutDlg dlg ;
   dlg.DoModal() ;
}

BOOL CTestDialog::OnInitDialog()
{
   CDialog::OnInitDialog() ;

   AfxBeginThread(MyThread, NULL) ;

   return TRUE ;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

david_johnsAuthor Commented:
Guys,
Thanks for the responses.  I was creating the dialog object inside the thread, but I was launching the thread with a CreateThread() instead of AfxBeginThread().  It works great after switching that.  Thanks for the help,
David
0
Roshan DavisCommented:
Better create in main thread, becoz, we are creating thread for some wait operation

If a thread is in *WAIT STATE* all the dialog messages(WM_PAINT, click etc etc) will block.

So Post a message to Main thread from the worker thread,
Map that message in main thread and create the dialog from there....





UINT YourThread(LPVOID lParam)
{
      CYourDialog *pDlg = (CYourDialog *)lParam;

      pDlg->PostMessage(WM_USER+10, 0, 0);

      REMAINING THREAD PROCESSES
}


And in your main Thread (in dialog class) put a message map like

BEGIN_MESSAGE_MAP(CYourDialog, CDialog)
      //{{AFX_MSG_MAP(CYourDialog)
      // class wizar added maps
      //}}AFX_MSG_MAP

      ON_MESSAGE(WM_USER+10, OnLaunchDialog)          <----- ADD LIKE THIS
END_MESSAGE_MAP()



void CYourDialog::OnLaunchDialog(WPARAM, LPARAM)
{
      CREATE YOUR DIALOG HERE      
}

Good Luck
0
inoxCommented:
Hi roshmon,
I hardly understand what you'r saying.
When a thread calls DoModal it hangs in a Messageloop, and code behind DoModal isn't executed until endmodal, so what could suspend the thread except a messagehandler of that dialog, and if, not to imagine this to be the mainthread.
Well modal dialogs in threads means also (as the thread could be created multiple times) that there cound be several modal dialogs in one app, what may look a little bit strange.
Also a simple Messagebox call from a thread should be harmless, I think, becaus they are usefull to hold the thread and decide for further processing.
I mention that, because I use similar in own apps and was conviced to handle well, but what you say makes me a bit pensive.
0
Roshan DavisCommented:
Thats Not for DoModal, Its for Create. I thought u are creating dialog *dynamically* using Create Function. Leave it. its for Modeless dialogs. leave it.
0
inoxCommented:
understood  ;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.