Solved

Progress Bar in a Dialog based application

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do you programatically show and hide the Windows 10 On-Screen-Keyboard? 3 908
string initialization in java 11 117
mapAB Challlenge 35 171
wordmultiple challenge 12 154
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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

713 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