Multithread and Dialog
When I press the OK button of the main dialog (CMainDialog), it will create
another dialog (CMyDialog) and a worker thread. Just before the worker
thread end, it will post WM_CLOSE message to CMyDialog. I have added a
breakpoint in both CMyDialog::OnClose and CMyDialog::OnNcDestroy. I supposed
it will stop just after the WM_CLOSE is sent. However, it is sent when the
Cancel button of CMainDialog is pressed. Any reason to explain that?
Also, since CMyDialog does not belong to the worker thread, it is good to
call UI related method CMyDialog::step (which call CProgressCtrl::StepIt)
directly from the worker thread?
Thanks.
UINT MyThreadProc( LPVOID pParam ) {
CMyDialog *pMyDialog = (CMyDialog*)pParam;
if (pMyDialog == NULL) {
return 1;
}
for (int i=0; i<5; ++i) {
::Sleep(1000);
pMyDialog->step();
}
pMyDialog->PostMessage(WM_CLOSE, 0, 0);
return 0;
}
void CMainDialog::OnBnClickedOk()
{
pMyDialog = new CMyDialog(5);
pMyDialog->Create(IDD_MY_DIALOG);
pMyDialog->ShowWindow(SW_SHOW);
AfxBeginThread(MyThreadProc, pMyDialog);
}
void CMyDialog::step() {
_progressBar.StepIt();
}
void CMyDialog::OnClose() {
DestroyWindow();
}
void CMyDialog::PostNcDestroy() {
CDialog::PostNcDestroy();
delete this;
}