开发者

Problem in displaying the message and the progress at the same time,threading in MFC?

I am working on threading in MFC..I am not sure how to use threading.. I cant attain what i excepted!What I actually tried, is to read the directory and display the file names and at the same time,the progress control should show the progress..I created a dynamic static to display the file names and progress bar control to show the progress...What happens here is,its first displaying all the file names one by one and then after that its showing the progress...so,am bit confused..can anybody explain me how to do that or开发者_C百科 refer me some articles...if u want i can post the code


This appears to be creating 10 threads for each file

        for(int i =0;i<10;i++)
        {
            THREADINFOSTRUCT *_param = new THREADINFOSTRUCT;
           _param->_this = this;
            AfxBeginThread(MyThread,_param,THREAD_PRIORITY_ABOVE_NORMAL);
            ....
        }

All 10 threads are simultaneously trying to set a single progress bar. (that's bad)

ts->_this->m_ProgressCtrl.SetRange (0, 500);
while (ts->_this->m_ProgressCtrl.GetPos () < 500)
{
    Sleep(250);
    ts->_this->m_ProgressCtrl.StepIt ();
}

We don't see the code for creation of the progress bar. But it presumably belongs the the main thread. So when these 10 threads try and SetRange or GetPos or StepIt, they are actually sending messages to the main thread to tell it to do those things.

Meanwhile the main thread is in the do {} }while(FindNextFileW(handle,&findData)); loop, and isn't pumping messages. So those threads all wait until the loop is finished and the main thread goes back to its message pump.

Then each of your threads wakes up and returns from calling SetRange, etc. But one at a time, since only message at a time can be processed by the main thread.

For this to work, you need to move the FindNextFile loop into a thread, and you need to let the main thread go back to the pump so that the UI can be updated while things progress.

You also need to stop trying to set the progress bar directly from any of your non-main threads. Instead use PostMessage to post messages back to the main thread and let it set the progress bar. That way your threads don't end up blocking and waiting for the main thread to talk to the progress bar on their behalf.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜