Solved

Progress Bar in a Dialog based application

Posted on 2000-03-08
4
347 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 31

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unix / Linux grid computing 5 149
ODBC Connection Logging, ADO.NET 6 56
fizzArray2 challenge 1 72
My project did see openJDK that I installed. What could be the problem 7 124
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 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

18 Experts available now in Live!

Get 1:1 Help Now