yeshengl
asked on
Creating a dialog in a thread
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::OnHelpStatisdl g()
{
CWinThread* pMyThread = AfxBeginThread(MyControlli ngFunction , 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.UpdateProgress Bar();
}
}
UINT CStatusApp::MyControllingF unction(LP VOID pParam)
{
// Create Modal dialog window to indicate the progress of the computation
((CStatusApp*)AfxGetApp()) ->m_cMyDia log.DoModa l();
return 1;
}
// Update the progress bar on the dialog
bool CStatusDialog::UpdateProgr essBar()
{
if(!GetSafeHwnd()) return false;
int i;
i = m_cProgress.GetPos();
i = i + 10;
m_cProgress.SetPos(i);
return true;
}
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::OnHelpStatisdl
{
CWinThread* pMyThread = AfxBeginThread(MyControlli
// 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.UpdateProgress
}
}
UINT CStatusApp::MyControllingF
{
// Create Modal dialog window to indicate the progress of the computation
((CStatusApp*)AfxGetApp())
return 1;
}
// Update the progress bar on the dialog
bool CStatusDialog::UpdateProgr
{
if(!GetSafeHwnd()) return false;
int i;
i = m_cProgress.GetPos();
i = i + 10;
m_cProgress.SetPos(i);
return true;
}
ASKER
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!
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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
Thanks a lot!
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..