We help IT Professionals succeed at work.

Cannot close CDialog that launched AfxBeginThread

Václav Jedlička
Václav Jedlička used Ask the Experts™
on
In a MFC app there is a CDialog that starts a lengthy process. I start a working process using this code:
AfxBeginThread(&QDlgUpgradeLib::Upgrade_Library, this);

Open in new window


Function
QDlgUpgradeLib::Upgrade_Library

Open in new window

does the processing and in the end it tries to close the CDialog by calling OnOK(), but that gives debug assertion "wincore.cpp line 932".

(platform toolset is v100)
assert.png
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Eduard GherguArchitect - Coder - Mentor

Commented:
Hi,

The thread that it's used to run QDlgUpgradeLib::Upgrade_Library cannot call the UIThread. You'll need to notify the UIThread about the finish of the background thread execution and close the dialog accordingly. You can have a look at https://www.codeproject.com/articles/2459/using-afxbeginthread-with-class-member-controlling.

Author

Commented:
Hi Eduard,

My code is similar to the sample you provided. The working thread notifies the dialog about progress so the dialog can update the progressbar.

I can update the progressbar, but cannot close the dialog.

Vaclav
Eduard GherguArchitect - Coder - Mentor

Commented:
Hi,
Can you provide more code, please?

Author

Commented:
here is the static method that does the processing
UINT QDlgUpgradeLib::Upgrade_Library(LPVOID pParam)
{
	QDlgUpgradeLib* pDlg = static_cast<QDlgUpgradeLib*>(pParam);

	//  here some processing

	// updates progressbar:
	pDlg->m_ctrl_progress_bar.StepIt();

	//then tries to close it:
	ap_dlg->OnOK();//crashes
}

Open in new window

Eduard GherguArchitect - Coder - Mentor

Commented:
Hi,
Who is ap_dlg? Shouldn't be pDlg on line 11?

Author

Commented:
yes, that is a typo
Vaclav
Eduard GherguArchitect - Coder - Mentor

Commented:
Hi,
Can you provide the code where the assertion has failed? Just to be sure that I’m looking to the same file like you

Author

Commented:
Here it is, it is line 932

Thank you
Vaclav Jedlicka
wincore.cpp
Eduard GherguArchitect - Coder - Mentor

Commented:
Hi,
What is the content of OnOK()?

Author

Commented:
It is part of MFC, I do not know what is inside.

I made a quick test project that demonstrates the issue. It contains just a dialog (MyDialog) with a thread.

it is here: https://github.com/proficad/thread_dlg

Thank you
Vaclav Jedlicka
Eduard GherguArchitect - Coder - Mentor

Commented:
Hi,
I was thinking that maybe you have added some custom code on the OnOK(). I'll have a look at your code.

Author

Commented:
I have found information in a book that CWnd derived objects cannot be accessed from various threads.

I fixed the issue by sending WM_CLOSE message to the dialog. Seems to work.

Thank you for your efforts!
Vaclav
Eduard GherguArchitect - Coder - Mentor

Commented:
Hi,

This is a kind of a hack. You'll need to do take a different approach for your implementation. The processing should not be performed on the dialog box, it should be used just to display the progress and other relevant information.

Author

Commented:
ok, then I do not know how.

Author

Commented:
I found another way and updated the test project.
Eduard GherguArchitect - Coder - Mentor

Commented:
Hi,
I'll have a look later.
I fixed the issue by sending WM_CLOSE message to the dialog.