I am trying to start a worker thread using AfxBeginThread
and wait until it finished. But I run into the issue, that I can either update the UI (show progress) without the thread conrol or wait for the thread but the UI freezes. I found out several approaches for getting the completion but they all use WaitForSingleObject
. Is there any other solution for this? Here is my example code:
UINT CProgressBarSampleDlg::MyControllingFunction(LPVOID pParam)
{
CProgressBarSampleDlg* pObject = (CProgressBarSampleDlg*)pParam;
for (size_t i = 0; i < 10; i++)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // simulate long running operation
pObject->m_ProgressBar.StepIt(); // update progress
}
return 0;
}
void CProgressBarSampleDlg::OnBtnStartClicked()
{
auto thread = AfxBeginThread(MyControllingFunction, this);
// ... this freezes the UI!
thread->m_bAutoDelete = FALSE;
WaitForSingleObject(thread->m_hThread, INFINITE);
delete thread;
}