
Showing a modeless progress dialog while opening document with MFC
Dear VC++ pandits,
I would like to display a progress dialog while opening a document in an MDI
application. The deserialization is done through a factory, and it's quite
an intricate (recurrent) process, so I cannot just have a loop like:
Read
while (!EOF)
{
DeserializeOneObject
Progress.Step
Read
Quote:
}
What I have done (please tell me if you have a "cleaner" solution) is that I
start a working thread in the OnOpenDocument like that:
BOOL MyDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
...
m_NrOfItems = 0;
m_isLoading = TRUE;
CWinThread* pThread = AfxBeginThread( ShowProgress, this ,
THREAD_PRIORITY_HIGHEST);
GetFactory().OpenDatabase( static_cast<RWCollectableString>( lpszPathName )
);
m_isLoading = FALSE;
...
Quote:
}
Where the ShowProgress function is implemented as follows:
UINT ShowProgress(LPVOID pParam)
{
MyDoc& Doc = *static_cast<MyDoc*>( pParam );
Doc.m_ProgressDlg.m_ItemsOpened.SetRange( 0, Doc.m_TotalNrOfItems );
try
{
while (Doc.isLoading())
{
Doc.m_ProgressDlg.m_ItemsOpened.SetPos( Doc.m_NrOfItems );
Doc.m_ProgressDlg.ShowWindow( SW_SHOWNORMAL );
Sleep( 200 );
}
Quote:
}
catch ( ... )
{
}
Doc.m_ProgressDlg.ShowWindow( SW_HIDE );
return 0;
Quote:
}
The m_NrOfItems attribute in the MyDoc class is incremented by each object
being loaded.
My problem is that the thread pauses at the:
Doc.m_ProgressDlg.m_ItemsOpened.SetRange( 0, Doc.m_TotalNrOfItems );
until the OnOpenDocument is finished (You will agree such a progress bar is
not that helpful;-). I guess some MFC objects must be locked through
Critical Sections. Is there a way to avoid that? Or do you have another
solution?
Otherwise the thread works fine if I replace the references to the dialog by
TRACE instructions.
I thank you for any information which can help me solve this problem.
Sincerely,
Mariano