Waiting to thread exit
Hi I am copyying file in a thread, meanwhile I am displaying a disloag with
progress bar, the problem is that message box is displayed before copying is
finished. Can anyone help me ??
CProgressDlg* pDlg = new CProgressDlg();
if ( ! pDlg ) return;
pDlg->Create( IDD_DIALOG1 );
pDlg->ShowWindow( SW_NORMAL );
CCopyThread* pThread = (CCopyThread* ) AfxBeginThread( RUNTIME_CLASS(
CCopyThread ), THREAD_PRIORITY_NORMAL,
0, CREATE_SUSPENDED );
if ( pThread )
{
pThread->pParent = pDlg;
pThread->ResumeThread();
pThread->CopyFile( "C:\\Test.rmvb", "D:\\Test.rmvb" );
}
AfxMessageBox(" Copy finished " );
///////////////////////////////////////////// Now thread class
MPLEMENT_DYNCREATE(CCopyThread, CWinThread)
CCopyThread::CCopyThread()
{
}
CCopyThread::~CCopyThread()
{
}
BOOL CCopyThread::InitInstance()
{
m_bAutoDelete = FALSE;
return TRUE;
}
int CCopyThread::ExitInstance()
{
// TODO: perform any per-thread cleanup here
return CWinThread::ExitInstance();
}
BEGIN_MESSAGE_MAP(CCopyThread, CWinThread)
ON_THREAD_MESSAGE( WMU_COPY_START, OnStartCopy )
ON_THREAD_MESSAGE( WM_USER_THREAD_COPY_FINISHED, OnCopyFinished)
END_MESSAGE_MAP()
// CCopyThread message handlers
void CCopyThread::CopyFile(const CString& strInFile, const CString&
outFile )
{
bool result = true;
if ( ! m_inFile.Open( strInFile, CFile::modeRead, NULL ) )
{
//::PostMessage( m_pDlg->GetSafeHwnd(), WM_USER_THREAD_COPY_FAILED, 0, 0 );
return;
}
if ( ! m_outFile.Open( outFile, CFile::modeCreate | CFile::modeWrite,
NULL ) )
{
//::PostMessage( m_pDlg->GetSafeHwnd(), WM_USER_THREAD_COPY_FAILED, 0, 0 );
return;
}
if ( result )
PostThreadMessage( WMU_COPY_START, 0, 0 );
}
void CCopyThread::CopyFileTh( CFile* in, CFile* out )
{
ULONGLONG size = in->SeekToEnd();//in->GetLength();
in->SeekToBegin();
DWORD bytesread = 0;
BYTE buffer[4096];
ULONGLONG readsize = 0;
//TRACE1(" Size is %d \n", size/1024 );
int procent = 0;
//::PostMessage( m_->GetSafeHwnd(), WM_USER_THREAD_COPY_STARTED, size, 1 );
//CWaitCursor wc;
do
{
//wc.Restore();
bytesread = in->Read( &buffer, 4096 );
out->Write( &buffer, bytesread );
readsize += bytesread;
procent = ( 100 * readsize ) / size;
::PostMessage( pParent->GetSafeHwnd(), WM_USER_THREAD_COPY_UPDATE_PROGRESS,
100, procent);
//TRACE2(" Copied %d of %d\n\n", readsize, size );
}
while ( bytesread > 0 );
in->Close();
out->Close();
}
//////////////////////////////////////////////////////////////////////////////
void CCopyThread::OnStartCopy(WPARAM wParam, LPARAM lParam)
{
//// Init action points
//m_iCurrentActionPoints = 0;
// Dangerous code
try
{
TRACE0("Copy Start\n");
// Start the copy process
CopyFileTh( &m_inFile, &m_outFile );
TRACE0("End\n");
// Send message to parent that we are ready
////PostMessageToParent(WMU_COPY_COMPLETE, 0, 0);
//DWORD dw = 0;
//if ( GetExitCodeThread( m_hThread, &dw ))
//{
// TRACE1(" Exit code is: %d\n", dw );
//}
//PostQuitMessage( dw );
PostThreadMessage(WM_USER_THREAD_COPY_FINISHED, 0, 0L );
}
catch (...)
{
// Send error message
//PostMessageToParent(WMU_ERROR, ERROR_COPYFILE, 0);
}
}
//////////////////////////////////////////////////////////////////////
void CCopyThread::OnCopyFinished(WPARAM wParam, LPARAM lParam)
{
AfxMessageBox(" Copy end message ");
pParent->ShowWindow( SW_HIDE );
DWORD dw = 0;
if ( GetExitCodeThread( m_hThread, &dw ))
{
TRACE1(" Exit code is: %d\n", dw );
}
PostQuitMessage( dw );
if ( GetExitCodeThread( m_hThread, &dw ))
{
TRACE1(" Exit code is: %d\n", dw );
}
//} while ( dw == STILL_ACTIVE );
//::Sleep( 2000 );
//::WaitForSingleObject( this->m_hThread, INFINITE );
}