CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2009
    Posts
    252

    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!!!!

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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.

  3. #3
    Join Date
    Feb 2009
    Posts
    252

    Re: Problem with CopyFileEx and Showing Progress

    Quote Originally Posted by Skizmo View Post
    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;

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    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

  6. #6
    Join Date
    Feb 2009
    Posts
    252

    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

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Problem with CopyFileEx and Showing Progress

    Quote Originally Posted by Alphadan View Post
    Problem Solved using this method

    http://www.edaboard.com/thread20874.html
    No, it is not "solved" although it could look like it was.

    Two points:
    1. As I already mentioned you should use PostMessage rather than SendMessage. Also read this J.Newcomer's essay Using Worker Thread
    2. You should use WM_APP range for user defined messages, not WM_USER one. Please, read about WM_APP in MSDN
    Victor Nijegorodov

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problem with CopyFileEx and Showing Progress

    Quote Originally Posted by Alphadan View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured