Problem with CopyFileEx and Showing Progress
CopyFileEx Doesnt return until it completes which made me think that i need to create a thread to allow posting the progress but when i use a thread
the UpdateData(FALSE); crashes the app
Anyadvice about whats wrong?
this is the code:
Struct to pass parameter to the thread
Code:
typedef struct tagTHREADPARAMS {
LPVOID pThis;
char Src[260];
char Dst[260];
} THREADPARAMS;
CODE
Code:
void CProgressFCopyDlg::OnButton1(){
THREADPARAMS ParamsPtr;
//Asignamos los Argumentos
ParamsPtr.pThis=this;
strcpy(ParamsPtr.Src,"F:\\GRID 2008-08-20 09-39-52-93C.avi");
strcpy(ParamsPtr.Dst,"C:\\GRID 2008-08-20 09-39-52-93C.avi");
//Creamos el Thread
DWORD ThreadID;
CreateThread(NULL,0,CopyFileProg,&ParamsPtr,0,&ThreadID);
if(ThreadID==0){
MessageBox("Error Creating Thread","Error");
}
}
DWORD WINAPI CopyFileProg(LPVOID lpParam){
THREADPARAMS* ParamsPtr = (THREADPARAMS*)lpParam;
CopyFileEx(ParamsPtr->Src,ParamsPtr->Dst,CopyProgressRoutine,ParamsPtr->pThis,NULL,0x00000001); //0x00000001 = COPY_FILE_FAIL_IF_EXISTS
return 0;
}
DWORD CALLBACK CopyProgressRoutine(LARGE_INTEGER TotalFileSize,LARGE_INTEGER TotalBytesTransferred,LARGE_INTEGER StreamSize,LARGE_INTEGER StreamBytesTransferred,DWORD dwStreamNumber,DWORD dwCallbackReason,HANDLE hSourceFile,HANDLE hDestinationFile,LPVOID lpData){
char B[50];
CProgressFCopyDlg* DlgPtr = (CProgressFCopyDlg*)lpData;
//Copied
formatn(TotalBytesTransferred.QuadPart,B);
DlgPtr->m_Copied=CString(B);
//Total
formatn(TotalFileSize.QuadPart,B);
DlgPtr->m_Total=CString(B);
//ProgressBar
DWORD ProgP=(TotalBytesTransferred.QuadPart / TotalFileSize.QuadPart)*100;
DlgPtr->m_ProgBar.SetPos(ProgP);
DlgPtr->UpdateData(FALSE); //THIS GIVES THE CRASH
return 0;
}
THX IN ADVANCE!!!!
Re: Problem with CopyFileEx and Showing Progress
Quote:
the UpdateData(FALSE); crashes the app
That is correct. Updating a window from another thread isn't really the way to go. You can send a message from the thread to the window to update the window.
Re: Problem with CopyFileEx and Showing Progress
Quote:
Originally Posted by
Skizmo
That is correct. Updating a window from another thread isn't really the way to go. You can send a message from the thread to the window to update the window.
Yea i was afraid that was the problem.
how might i update that window from the thread without using UpDateData()
Thx for the answer;
Re: Problem with CopyFileEx and Showing Progress
Code:
CProgressFCopyDlg* DlgPtr = (CProgressFCopyDlg*)lpData;
DlgPtr->SendMessage (some_user_defined_message);
And in your CProgressFCopyDlg dialog you handle the message and there you can update your dialog.
Re: Problem with CopyFileEx and Showing Progress
Skizmo is right except the PostMessage should be used rather than the SendMessage. With the SendMessage you could get a dead lock.
Re: Problem with CopyFileEx and Showing Progress
Problem Solved using this method
http://www.edaboard.com/thread20874.html
u gave me the key Skizmo Thx both of you.
Reps added
Re: Problem with CopyFileEx and Showing Progress
Quote:
Originally Posted by
Alphadan
No, it is not "solved" although it could look like it was.
Two points:
- As I already mentioned you should use PostMessage rather than SendMessage. Also read this J.Newcomer's essay Using Worker Thread
- You should use WM_APP range for user defined messages, not WM_USER one. Please, read about WM_APP in MSDN
Re: Problem with CopyFileEx and Showing Progress
Quote:
Originally Posted by
Alphadan
CopyFileEx Doesnt return until it completes which made me think that i need to create a thread to allow posting the progress but when i use a thread
Granted I haven't used it extensively and don't know if this will apply to you, but have you considered using the SHFileOperation?
http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
Look at the lpszProgressTitle member of the SHFILEOPSTRUCT structure.
Regards,
Paul McKenzie