|
-
April 30th, 2012, 10:15 AM
#1
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!!!!
-
April 30th, 2012, 10:49 AM
#2
Re: Problem with CopyFileEx and Showing Progress
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.
-
April 30th, 2012, 11:01 AM
#3
Re: Problem with CopyFileEx and Showing Progress
 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;
-
April 30th, 2012, 11:04 AM
#4
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.
-
April 30th, 2012, 11:10 AM
#5
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.
Victor Nijegorodov
-
April 30th, 2012, 11:59 AM
#6
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
-
April 30th, 2012, 12:41 PM
#7
Re: Problem with CopyFileEx and Showing Progress
 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
Victor Nijegorodov
-
April 30th, 2012, 02:16 PM
#8
Re: Problem with CopyFileEx and Showing Progress
 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|