Solved

Creating a dialog in a thread

Posted on 2002-07-18
5
258 Views
Last Modified: 2013-11-20
Hi,

I have a question regarding to create a dialog in a thread. My design is: when the main program is about to start a lengthy compuataion, it new a thread and a dialog window is launched from the newly created thread. The lenghty computation is kept in the main program.

But this design does not work. It seems the diaog is not completely created before it switch from the thread to the main program thread. So when I call UpdateProgressBar() from the main program thread, it will simply return with false. I tried to do things in reverse: do lenthy computation in the newly created thread and create the dialog box in the main program thread, and it works well. But to be consistent, I need to do it like what I am doing now. Can anyone help me in this problem? Thanks!

Here is my code:

---------------------------------------------------------
void CStatusApp::OnHelpStatisdlg()
{

     CWinThread* pMyThread = AfxBeginThread(MyControllingFunction, NULL, THREAD_PRIORITY_NORMAL);

     // Do some lenthy compuataion and updage the progress bar on the dialog
     for(int n=0; n<10; n++)
     {
          Sleep(100);

          // Update the progress bar
          m_cMyDialog.UpdateProgressBar();
     }
}

UINT CStatusApp::MyControllingFunction(LPVOID pParam)
{
     
     // Create Modal dialog window to indicate the progress of the computation
     ((CStatusApp*)AfxGetApp())->m_cMyDialog.DoModal();

     return 1;
}

// Update the progress bar on the dialog
bool CStatusDialog::UpdateProgressBar()
{
     if(!GetSafeHwnd())     return false;

     int i;
     i = m_cProgress.GetPos();
     i = i + 10;
     m_cProgress.SetPos(i);

     return true;
}
0
Comment
Question by:yeshengl
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:mblat
ID: 7163651
If I am not mistaken to create dialog in the thread that has to be UI thread, not worker thread, meaning thread has to have ability to windows process messages.

Keeping dialog in main app, and moving calculation into the separate thread, like you tried seems to be "standard" way to avoid this problem ( if there is such a thing as standard solution :-) )

You can look at
http://codeguru.com/controls/

they have several exapmles for what your are trying to do
like this:
http://codeguru.com/controls/progress_wnd.shtml

also try

codeproject.com

Hope it helps..
0
 

Author Comment

by:yeshengl
ID: 7163862
Hi mblat,

Thanks for your help! I have reviewed the links you provided and they are very good examples. Some of the examples are quite close to what we need.

Now, I am just curious: why my code( do calculation in main program and launch dialog in the newly created thread) does not work? And how to fix it? It will be rewarding to me if I know why.

Thanks a lot!
0
 
LVL 4

Accepted Solution

by:
mblat earned 50 total points
ID: 7163976
Well, the reason for that is that you are attempting to create UI object in a thread.  Certain steps need to be taken to do that.
Look for articles on msdn
Multithreading: Creating Worker thread and
Multithreading: Creating User-Interface Threads

to see the difference.  Like I mentioned creating UI threads not a trivial task ( at least when compared with creation of worker threads)  That is why in most cases (at least in my, I admit, limited expirience with UI threads ) people go different routes from you: they create worker thread to perform calculations and keep UI in main apllication thread.  That is not to say that doing it otherwise is wrong - it is not, but usually it is more headache than it worth.
0
 

Expert Comment

by:fengyun
ID: 7164178
if you have MSDN, you can get some sample in 'vc98 \\MFC\\ADVANCED\\' folder,they are MTMDI,MTGDI,MTRECALC and mtexes. they are very good examples.
0
 

Author Comment

by:yeshengl
ID: 7166139
Thanks a lot!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
isEverywhere  challenge 19 72
tripleUp challenge 7 73
EvenOdd challenge 10 88
Detect file exist or not 3 128
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

919 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now