How to show a progress bar in a dialog?

I tried to show the progress bar in a dialog. When OnInitDialog()  is called, Progress bar is called at the same time. But the bar cannot be shown correctly as it cannot be shown until it finishes loading the progress bar. The code is as follows:
BOOL CEncodeProgressDlg::OnInitDialog()
{
      CDialog::OnInitDialog();
      
      // TODO: Add extra initialization here
CProgressCtrl* m_enc = (CProgressCtrl*)                            GetDlgItem(IDC_ENCPROGRESS);
      m_enc->SetRange( 0, 10 );
              m_enc->SetStep( 1 );

      for( int i = 0; i < 10; ++i )
    {
            int nSleepTime = MulDiv( 100, rand(), RAND_MAX );
            Sleep( nSleepTime );

            // 5 - Update progress control
            m_enc->StepIt();
    }
       
      return TRUE;  // return TRUE unless you set the focus                                  //to a control
                    // EXCEPTION: OCX Property Pages should return FALSE
}
makoto_liangAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

freshmeatCommented:
wow, cool idea, i wanna know how to?
:D
0
sboucher789Commented:
The way the code is written here prevents the dialog to refresh the progress bar because that dialog's thread is either sleeping (Sleep(...)) or if processing stuff (Muldiv(...)).

You could manually refresh the dialog and/or progress bar by calling m_enc->RedrawWindow() and/or this->RedrawWindow().

Otherwise, you need to put your loop in a separate thread and have this thread post user messages to your dialog so it can update the progress bar. The dialog will have plenty of time to properly update your progress bar during the sleep of the second thread.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.