Solved

Progress Bar in a Dialog based application

Posted on 2000-03-08
4
346 Views
Last Modified: 2013-11-20
My application is a dialog based application in VC++ 6.0

On click of a button i am creating a modeless dialog consisting of a static text field,a ProgressBarControl and a cancel button.

From the main dialog i am using a for loop to increment the progress bar and check if the user has pressed the cancel button.

Everything works fine except that the for loop does not seem to release control and the user cannot press the cancel button.
I dont understand why the progress dialog does not reconise any events during the for loop in the main dialog.

below is the code

void CMainDialog::OnClickProgButton()
{
Demo* pDialog;
pDialog = new Demo(this);
BOOL dd=pDialog->Create();
      
pDialog->SetRange(0,50000);
for(int i=0;i<50000;i++)
{
  pDialog->StepIt();
  if(pDialog->IsCancelled())
  {
    MessageBox("Cancelled","Error");
    break;
  }
}
      
pDialog->DestroyWindow();
}
0
Comment
Question by:mickoo
4 Comments
 
LVL 8

Expert Comment

by:VinExpert
ID: 2595618
Hi,

I would suggest to put the code for the updation of the progress bar in the dialog, where it has been put. So as soon as user presses cancel, U can abort it. That seems to be the simple and best way. Whay can't u go for that?

VinExpert
0
 
LVL 30

Accepted Solution

by:
Zoppo earned 100 total points
ID: 2595672
Hi mickoo,

This is because the for-loop blocks the message processing. Such lengthy loops should be avoided to allow the application the handling of messages. You can try one of the following:

- Instead of a for-loop try to do such lengthy calculation either in idle time (see WM_ENTERIDLE and CWinApp::OnIdle()) or in a thread (see AfxBeginThread())

- Inside the loop check message queue for new messages and process them, i.e. like this:

MSG msg;
for(int i=0;i<50000;i++)
{
 if ( i % 100 == 0 )
 {
  // here every 100th loop we check message queue
  while ( ::PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
  {
   ::DispatchMessage( &msg );
  }
 }
 pDialog->StepIt();
 if(pDialog->IsCancelled())
 {
  MessageBox("Cancelled","Error");
  break;
 }
}


hope that helps,

ZOPPO
0
 
LVL 1

Expert Comment

by:Sadhu
ID: 2595988
0
 

Author Comment

by:mickoo
ID: 2599253
Thanks a lot Zappo

your suggestion worked perfectly
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now