views:

60

answers:

1

In my application I have a simple module were I will read files for some process that will take few seconds..so I thought of displaying a progress bar(using worker thread) while the files are in progress.I have created a thread (code shown below) and also I designed a dialog window with progress control.I used the function MyThreadFunction below to display the progressbar but it just shows only one time and disappears,I am not sure how to make it work.I tried my best inspite of the fact that I am new to threading.

 reading files
    void ReadMyFiles()
    {

       for(int i = 0; i < fileCount ; fileCount++)
        {   
        CWinThread* myThread = AfxBeginThread((AFX_THREADPROC)MyThreadFunction,NULL);
        tempState = *(checkState + index);
        if(tempCheckState == NOCHECKBOX)
        {
            //my operations
        }
        else//CHECKED or UNCHECKED
        {
            //myoperation
        }
        myThread->PostThreadMessage(WM_QUIT,NULL,NULL);
        }
    }

    thread functions
    UINT MyThreadFunction(LPARAM lparam)
    {
        HWND dialogWnd = CreateWindowEx(0,WC_DIALOG,L"Proccessing...",WS_OVERLAPPEDWINDOW|WS_VISIBLE,
                        600,300,280,120,NULL,NULL,NULL,NULL);
        HWND pBarWnd =  CreateWindowEx(NULL,PROGRESS_CLASS,NULL,WS_CHILD|WS_VISIBLE|PBS_MARQUEE,40,20,200,20,
                                dialogWnd,(HMENU)IDD_PROGRESS,NULL,NULL);

        MSG msg;

        PostMessage( pBarWnd, PBM_SETRANGE, 0, MAKELPARAM( 0, 100 ) );
        PostMessage(pBarWnd,PBM_SETPOS,0,0);
        while(PeekMessage(&msg,NULL,NULL,NULL,PM_NOREMOVE))
        {
            if(msg.message == WM_QUIT)
            {
                DestroyWindow(dialogWnd);
                return 1;
            }
            AfxGetThread()->PumpMessage();
            Sleep(40);
        }
        return 1;


    }
+1  A: 

Do you really want to create a new thread and a progress bar for each individual file? Create the thread outside of the for() loop.

But this is not the right way to do it, your main UI is still dead as a doornail. Windows will turn your main window in a ghost with "Not Responding" in the title bar after a couple of seconds. You want to use a worker thread to do the file manipulation and the main thread to display the progress bar using a dialog that can only be closed when the worker uses PostMessage() to indicate completion.

Hans Passant