views:

112

answers:

2

I have an MFC application. It is basically just copying files from one drive to another. When I'm copying large files (more than 1 Gb) and I click on my window, my application freezes, but the copying proceeds in the background. Someone told me that I have to use a worker thread. I used it, but it still freezes. When I'm copying small files then it is OK. I can't figure out what could be the problem. Please someone help!

Here's my code:

void CGetFileListDlg::OnBnClickedButtonGetFileList()  
{
 //here i'm doing file list comparing
 AfxBeginThread( CopyThread, &Tstruct ); //here i call my thread and give a struct to it as a paramter, which contains, which file i have to copy
}   

UINT CopyThread( LPVOID pParam )
{
UINT uiMaxPass = 3;
UINT uiPAssCount = 0;
int i = 0;

threadstruct *Test = (threadstruct*)(pParam);
CGetFileListDlg* ptr = (CGetFileListDlg*)Test->ez ;

struct address
{
    char *from;
    char *to;
    int current;
};

struct address Address;


for ( i = 0; i < Test->diff; i++ )
    {

    TCHAR currentfile[512], file[MAX_PATH +32], successf[10], unsuccessf[10], buf[20], remainingf[20], oprog[10];
    char tmp[1024], tmp2[1024],dest[1024];
    int j,k,l;
    char ch;

    memset( tmp, 0, sizeof( tmp ) );
    memset( dest, 0, sizeof( dest ) );
    memset( tmp2, 0, sizeof( tmp2 ) );
    memset( buf, 0, sizeof( buf ) );
    memset( currentfile, 0, sizeof( currentfile ) );
    memset( file, 0, sizeof( file ) );
    memset( successf, 0, sizeof( successf ) );
    memset( unsuccessf, 0, sizeof( unsuccessf ) );
    memset( remainingf, 0, sizeof( remainingf ) );
    memset( oprog, 0, sizeof( oprog ) );
    ch = NULL;
    strcat( dest, SecondaryHDD );

    l = 1;
    for ( k = strlen( SecondaryHDD ); k < strlen( Test->difference[i].filename ); k++ )
    {
        dest[k] = Test->difference[i].filename[ l + strlen( SecondaryHDD ) - 1 ];
        l++;
    }
    dest[k]='\0';

    for ( j = strlen( Test->difference[i].filename); ch != '\\'; j-- )
    {
        ch = Test->difference[i].filename[j];
    }

    l = 0;
    for ( k = 3; k < j + 1; k++ )
    {
        tmp2[l] = Test->difference[i].filename[k];
        l++;
    }
    tmp2[l]='\0';
    strcpy( tmp, SecondaryHDD );
    strcat( tmp, tmp2);

    SHCreateDirectoryExA( NULL, tmp, NULL );

    memset( file, 0, sizeof( file ) );

    memset( tmp, 0, sizeof( tmp ) );
    strcpy(tmp, Test->difference[i].filename );

    MultiByteToWideChar(  CP_ACP, NULL, tmp, -1, file, strlen( Test->difference[i].filename ) );
    wsprintf( currentfile, _T("%s"), file );
    ptr->m_edCurrentCopy.SetWindowText( currentfile );

    Address.from = strdup(tmp);
    Address.to = strdup(dest);
    Address.current = i;

    PostMessage( (HWND)Test->hWnd , WMU_PROGRESS, (WPARAM)&Address, (LPARAM)&dest ); //calling OnProgressMsg function, which does the copy
    Sleep(100);
}

PostMessage( (HWND)Test->hWnd, WMU_COPYDONE, uiPAssCount, 0 );

return 0;
}  

LRESULT CGetFileListDlg::OnProgressMsg( WPARAM wParam, LPARAM lParam )
{ 
    TCHAR currentfile[512], file[MAX_PATH +32], successf[10], unsuccessf[10], buf[20], remainingf[20], oprog[10];
char tmp[1024], tmp2[1024],dest[1024];
int j,k,l;
char ch;
struct address
{
    char *from;
    char *to;
    int current;
};

address *Address = (address*)wParam;

//char *to = (char*)lParam;    
//char *from = (char*)wParam;    

int ret = 0;
ret = CopyFileA( Address->from, Address->to, false );
//ret = CopyFileExA( Address->from, Address->to, &MyCopyProgressRoutine, this, FALSE,FALSE);

if ( ret == 0 ) //failed
{
    wsprintf( buf, _T("Failed ( %d )"), GetLastError() );
    m_difference.SetItemText(Address->current, 2, buf);
    unsuccess++;
    wsprintf( unsuccessf, _T("%d"), unsuccess, GetLastError() );
    m_unsuccess.SetWindowText( unsuccessf );
}
else
{
    m_difference.SetItemText(Address->current, 2, L"OK!");
    success++;
    wsprintf( successf, _T("%d"), success );
    m_success.SetWindowText( successf );
}

wsprintf( remainingf, _T("%d"), ( diff - ( success + unsuccess ) ) );
m_remaining.SetWindowText( remainingf );

wsprintf( oprog, _T("%d %%"), ( (success + unsuccess ) *100 )/diff );
m_overallprog.SetWindowText( oprog );

UpdateData(FALSE);

return 0;
}
+1  A: 

I think what was meant by use a WorkerThread is that the WorkerThread must be doing the copying. To talk in terms of Win32API at least, all Windows, in fact anything with a hWnd handle in Windows, has a message loop. When this message loop stops responding within a sufficient timeframe, Windows Explorer thinks your application is "not responding" because in essence it isn't - it isn't processing messages.

What you need to do is invoke the subthread which does the copying, then notify the "parent" thread when done so that the dialog can be closed; or notify the parent dialog of progress so that the parent dialog can update a progress bar, or something like that.

Ninefingers
+10  A: 
PostMessage( (HWND)Test->hWnd , WMU_PROGRESS, (WPARAM)&Address, (LPARAM)&dest );
//calling OnProgressMsg function, which does the copy

So you spawn a new thread, that ... posts messages to the main thread and gets the main thread to do all the copying?

That's not how you use worker threads. Your worker thread should be the one doing the copying, and all that OnProgressMsg should do is update the UI.

Anon.
Hi! If i i understand you correctly, than in my CopyThread function, should i do the copying, and in the OnProgressMsg function i should just update the GUI? Am i right?
kampi
@kampi: Yep. That's exactly it.
Anon.
Oh, man! Thank you very much! This was my problem. Now it works fine :) Thank you again :)
kampi