Updating a progress bar during long calculations

 

Problem: FMX progress bar won’t update

Solution tested on C++ Builder 10.2

Suppose you’re doing a long calculation that doesn’t involve FireMonkey components. You’d like to update the user with a progress bar. You periodically take a break in the computations to update the progress bar. However, when you run your program, the progress bar doesn’t move.

The solution is to call Application->ProcessMessages() after updating the progress bar. Application is a global variable present in all FireMonkey programs. The code below is an example of the problem and its solution. (By the way, this problem also occurs with the animation indicator and other GUI elements such as a label, and you can solve it with the same technique.)

const int update_percent = 5;
const int num_loops = 100000;
int update_interval = static_cast<int>( num_loops(update_percent/100.0) );
if( update_interval == 0 )
  update_interval = 1;

int percent = 0;
for( int i = 0; i < num_loops; ++i )   
{         
  if( i % update_interval == 0 )         
  {               
     percent += update_percent;           	
     ProgressBar1->Value = percent;
     Label1->Text = String().sprintf( L"Finished %d%% of work", percent );
     Application->ProcessMessages();
  }

  // do lots of work, e.g.
  int s = 0;
  for( int j = 0; j < 10000; ++j )
    s += j;
}

In the above snippet, we have a progress bar ProgressBar1 and a label Label1. The progress bar will show the percentage of the loop that we’ve finished and the label will display the same information in words. Here’s what’s happening on various lines:

  • Line 1 – update the display every time we finish another 5% of the loops
  • Line 3 – compute how many loop iterations the percentage is equivalent to
  • Lines 4-5 – make sure the number of iterations is not zero, or the remainder operation in Line 10 will fail
  • Line 10 – every time we hit another 5% of the iterations, we update the display
  • Lines 13-14 – store the new percentage and text
  • Line 15 – this causes the label and progress bar to display the new values
  • Lines 19-21 – simulate a lengthy computation
Advertisements

Tutorial Part 2- A Portable Date Library for C++

In the previous part of this tutorial we downloaded and set up Howard Hinnant‘s date library. This is a library written in standard C++ that lets us work with dates much more easily than what we can currently do. We learned how to work around some potential compiler errors and how to create compile-time and run-time dates. In this part of the tutorial we’ll discuss the library’s error handling and efficiency, and then concentrate on doing calculations with the dates.

Continue reading “Tutorial Part 2- A Portable Date Library for C++”

Tutorial Part 1- A Portable Date Library for C++

Introduction

How do we work with dates and times in C++? One way is to use the C date and time library. This library has functions for manipulating times, such as time, clock and difftime; functions for converting formats of times and dates, e.g., gmtime, localtime, mktime; and various types, such as tm for calendar time, time_t for the time since the epoch, and clock_t, which denotes a process running time. In addition, since C++11 we’ve had a nice library for dealing with points in and durations of time, namely, chrono.

These libraries do have their problems though. The C library (not surprisingly) looks like just that – a C library, and not like C++ code. For example, it has no classes, there are no operators for performing such useful functions as determining the amount of time between two dates or whether one date comes before or after another date, and the internals of the library are exposed and often clumsy to work with. The chrono library overcomes these deficiencies because it was designed from the ground up to be in C++. Unfortunately, chrono has no facilities for working with calendar dates.

Continue reading “Tutorial Part 1- A Portable Date Library for C++”