Note that you must also override the virtual methods OnOK and OnCancel to have empty
bodies, so that a mis-typed <enter>or <esc> will not terminate the dialog. Then you have
to add an OnClose handler. The correct code would now be
OnInitDialog:
running = TRUE;
AfxBeginThread(StartThread, this);
in your thread
CDialog* dlg = (CDialog*)pParam;
...do thread stuff
dlg->PostMessage(UWM_THREAD_IS_FINISHED);
Note that typically your thread function would be declared as a static method of the
CMyAppDlg class. See my essay on worker threads to see how easy it is to move back from C
space to C++ space. It's on my MVP Tips site.
OnClose:
if(running)
{
...request thread termination here, same as your stop button would do
return;
}
CDialog::OnOK();
LRESULT CMyAppDlg::OnThreadIsFinished(WPARAM, LPARAM)
{
running = FALSE;
PostMessage(WM_CLOSE);
return 0;
}
This is the standard "asynchronous shutdown" pattern.
對話框和工作線程同步工作的標準模型
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.