Using background threads for long processes in MFC: Background Thread 2
We can solve the problem of the memory leak, and of the premature destructor call, by simply blocking until the thread is finished:
void CBgthreadDlg::OnBackgroundJoin()
{
CWaitCursor wait;
TaskThread *t = new TaskThread;
t->Start();
// Wait for it to finish
t->Join();
delete t;
}
This is, in effect, no better than any of our foreground thread examples, but it does get us a step closer to the ideal solution.
The source so far: bgthread_source3.zip.