Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Launching a dialog from a secondary thread

Posted on 2003-10-21
8
Medium Priority
?
365 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:david_johns
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 4

Expert Comment

by:inox
ID: 9595229
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
 

Expert Comment

by:SGyves
ID: 9595503
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
 
LVL 9

Accepted Solution

by:
Priyesh earned 500 total points
ID: 9596506
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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 

Author Comment

by:david_johns
ID: 9596623
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
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9596756
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
 
LVL 4

Expert Comment

by:inox
ID: 9597763
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
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9597792
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
 
LVL 4

Expert Comment

by:inox
ID: 9597952
understood  ;-)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question