How to show a modal dialog in multithread app?
-
How to show a modal dialog in multithread app? I want to show a modal dialog in multi thread application. But when I show the dialog as the fllowing, VC shows "Debug Assertion Failed" dialog. Anyone knows how to do it? Thanks very much. CAboutDlg dlg; dlg.DoModal(); The attachment is a demo. testmultithread.zip
-
How to show a modal dialog in multithread app? I want to show a modal dialog in multi thread application. But when I show the dialog as the fllowing, VC shows "Debug Assertion Failed" dialog. Anyone knows how to do it? Thanks very much. CAboutDlg dlg; dlg.DoModal(); The attachment is a demo. testmultithread.zip
Hi LaoWei. First, there is a little bug in your
CreateMultiThread
:void CTestMultiThreadApp::CreateMultiThread()
{
DWORD tID;
g_nIndex**[1]** = 0;
::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc, (LPVOID)&(g_nIndex**[0]), 0, &tID);
g_nIndex[1]** = 1;
::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc, (LPVOID)&(g_nIndex**[1]**), 0, &tID);
}Second, you use
CreateThread
in conjunction with MFC. This is dangerous. If your thread uses MFC support it should always be created viaAfxBeginThread
. Knowing this, you can writeCreateMultiThread
as follows:void CTestMultiThreadApp::CreateMultiThread()
{
DWORD tID;
g_nIndex[0] = 0;
AfxBeginThread((AFX_THREADPROC)afxThreadProc,(LPVOID)&(g_nIndex[0]));g\_nIndex\[1\] = 1; **AfxBeginThread**((AFX\_THREADPROC)afxThreadProc,(LPVOID)&(g\_nIndex\[1\]));
}
Now your "Debug Assertion Failed" is gone bye bye... :) RK