If you've ever created an interactive application that processes a lot of data, and as a result runs for a long time, you've probably had situations where a user thought that the application was hung. While your first instinct might be to go into a long explanation of how applications on the System i don't "hang" like Windows applications do, there is another option. Giving simple user feedback for situations like this can help prevent the perception that your code is inefficient and takes too long. In this article, we'll examine how to do this with a simple RPGLE subprocedure.
Making Progress
The key to an application that can realistically support a progress bar is that the application itself has to be processing the data. What I mean by this is that an application that hands off reading and processing of the data to another application or that experiences a long wait time on an SQL EXEC, for example, wouldn't be a good candidate for this type of approach. Ideally, you would want to present a progress bar for a process that reads data from either a database file or another source, such as a Web service or Excel file, and performs some sort of processing with that data.
In each circumstance, you would have to have some way to determine the number of records to be processed prior to beginning your processing. This would be used to determine how far through the process you are. An example might be a process that generates and sends statements to customers. Prior to executing the process, you would determine the number of records in the statements database file and pass that into the RPGLE program that generates the statements. This value would then be used to determine, based on the number of records read, what percent of the process is complete. This information can then be passed to the progress bar subprocedure to generate and update the progress bar display.
The complete source for this example can be found here.
PROGBAR Subprocedure
To start off, let's examine the code for the procedure prototype for the PROGBAR subprocedure. This code is shown below.
* Subprocedure prototypes for PROGBAR service program.
*
* These prototypes are used to create and display a progress bar
* within an RPGLE program.
*
* Constants
d COLOR_RED c CONST(x'A9')
d COLOR_WHITE c CONST(x'A3')
d COLOR_BLUE c CONST(x'BB')
d COLOR_PINK c CONST(x'B9')
d COLOR_GREEN c CONST(x'A1')
* Create a new progress bar
d createProgressBar...
d PR *
* Display a previously defined progress bar
ddisplayProgressBar...
d PR 1N
d barObjOPtr *
* Remove a previously created progress bar
d destroyProgressBar...
d PR
d barObjPtr *
d prgBars DS dim(10) qualified
d barActive 1N
d barIndex 5 0
d Percent 3 0 inz(0)
d StatusMessage 75
d WindowTitle 20
d Color 1A inz(COLOR_GREEN)
d ShowPercent 1N Inz(*ON)
d WindowTop 2 0 inz(1)
Note that this prototype member contains constant definitions for colors to be used to define the color of the progress bar itself. This source also defines the procedure prototype for the following three subprocedures:
- createProgressBar--This routine simply creates a pointer for a new progress bar object.
- displayProgressBar--This procedure is used to display or update the display of our progress bar object.
- destroyProgressBar--This procedure removes the progress bar from memory. If no active progress bars remain, this procedure also closes the screen containing the progress bar window.
Because of the way that the progress bar objects are defined, it's actually possible to have multiple progress bars active within the same program; for example, you may want to have one bar that indicates progress for subtasks within a larger process and another that indicates progress for the entire process. The prgBars data structure is used to define the attributes of our progress bar. These attributes are listed in the table below.
Attributes Used to Define a Progress Bar
|
|||
Attribute Name
|
Description
|
Type
|
Length
|
barActive |
An indicator that identifies whether or not the progress bar is still in use |
Indicator (on/off) |
1 |
barIndex |
Counter used as the unique identifier for the progress bar |
Numeric |
5,0 |
Percent |
Percent value (0-100) used to define the length of the progress bar shown to indicate task completeness |
Numeric |
3.0 |
StatusMessage |
Text value used to display an optional status message within the progress bar window |
Alphanumeric |
75 |
WindowTitle |
Text used as the title for the progress bar window. |
Alphanumeric |
20 |
Color |
Used to indicate the color for the progress bar itself using one of the defined color constants |
1-character hex value |
1 |
ShowPercent |
An indicator used to identify whether or not to display the percentage value within the progress bar |
Indicator (on/off) |
1 |
WindowTop |
Indicates top line position for the status bar window. Note this must be set to a value small enough to allow the entire window to fit on the display. |
Numeric |
2,0 |
The data structure containing these values can be associated with multiple program variables, effectively giving you the ability to display multiple progress bars from within a single program. Some of these values, like the Color and ShowPercent values, will be set when the bar is initially created. Others, such as the Percent and StatusMessage values, can be changed each time the status bar is displayed. Note that the Color attribute is defined using one of the five color constant values shown in the table below:
Color Constant Values
|
|
Color Constant
|
Description
|
COLOR_RED
|
Red progress bar |
COLOR_WHITE
|
White progress bar |
COLOR_BLUE |
Blue progress bar |
COLOR_PINK |
Pink progress bar |
COLOR_GREEN |
Green progress bar (default value) |
The source for the PROGBAR service program is shown below:
h nomain BNDDIR('PROGBAR')
fprogbarfm cf e workstn USROPN
f infds(scr_infds)
/copy qrpglesrc,progbarpr
d pos s 3 0
d bars S like(prog) inz(*ALLx'4f')
d rtnVal S 1N
d scr_close S 1N
d barCount S 10I 0
dscr_infds ds
d scr_open 9 9N
P createProgressBar...
P B EXPORT
d createProgressBar...
d PI *
d rtnVal S *
d ix S 5 0
/free
rtnval = *NULL;
for ix = 1 to %elem(prgBars);
if not prgBars(ix).barActive;
prgBars(ix).barActive = *ON;
prgBars(ix).barIndex = ix;
rtnval = %addr(prgBars(ix));
leave;
endif;
endfor;
if not scr_open;
open progbarfm;
endif;
return rtnVal;
/end-free
p createProgressBar...
P E
PdisplayProgressBar...
P B EXPORT
ddisplayProgressBar...
d PI 1N
d barObjPtr *
d objProgBar DS based(barObjPtr) Likeds(prgBars)
/free
if not scr_open;
open progbarfm;
scr_close = *ON;
endif;
// create new bar
if objProgBar.barActive;
COLOR = objProgBar.Color;
sline = objProgBar.WindowTop;
title = objProgBar.WindowTitle;
if title = '';
title = 'Progress Bar';
endif;
pos = (objProgBar.Percent/100)*75;
prog = %subst(bars: 1: pos);
status = objProgBar.StatusMessage;
if pos<1;
pos = 1;
endif;
if objProgBar.ShowPercent = *ON;
%subst(PROG:37:4) = %EDITW(objProgBar.Percent:' 0%');
endif;
%subst(PROG:pos:1) = x'24';
write progbarw;
endif;
if scr_close;
close progbarfm;
endif;
Return rtnVal;
/end-free
PdisplayProgressBar...
P e
P destroyProgressBar...
P B EXPORT
d destroyProgressBar...
d PI
d barObjPtr *
d lclBar DS BASED(barObjPtr) likedS(prgBars)
d
d rtnVal S 1N
d ix S 5 0
d activeBars S 1N
/free
activeBars = *OFF;
if barObjPtr <> *NULL;
lclBar.barActive = *OFF;
for ix = 1 to %elem(prgBars);
if prgBars(ix).barActive;
activeBars = *ON;
endif;
endfor;
if scr_open and not activeBars;
close progbarfm;
*INLR = *ON;
endif;
endif;
return;
/end-free
P destroyProgressBar...
P e
The functionality performed by this service program is fairly straightforward. We use a 75-byte field as the progress display. That field is manipulated using the hex codes for specific reverse-image color attributes. The hex display attribute value used to turn off reverse-image is then used to end the progress bar. The result is that we end up with a portion of the 75-byte field displayed in reverse-image to indicate percent of completeness.
To utilize this functionality, we must first initialize our new progress bar using the createProgressBar subprocedure. The subprocedure accepts no parameters, but returns a pointer to the newly created progress bar object. Next, we define the values for our progress bar using a data structure defined like the prgBars data structure. Note that this data structure is what the pointer created by the createProgressBar subprocedure points to. Each time we want to display (or update) our progress bar, we use the displayProgressBar subprocedure. This subprocedure accepts a single parameter, which is the resulting pointer returned by the createProgressBar subprocedure. Once a given progress bar is no longer needed, use the destroyProgressBar subprocedure to deactivate the progress bar itself and perform any additional cleanup task, including closing the display file and setting on LR when all progress bars have been destroyed.
Putting It to Use
To better illustrate how to utilize the PROGBAR service program, I've included the sample program shown below:
H DFTACTGRP(*NO) BNDDIR('PROGBAR')
Ftestscr cf e workstn
/copy qrpglesrc,progbarpr
D x s 3 0
D x1 s 13 0
D pcta s 5
D progBar1Obj S like(createProgressBar)
D progBar2Obj S like(createProgressBar)
D progBar3Obj S like(createProgressBar)
D progBar1 DS likeDS(prgBars) based(progBar1Obj)
D progBar2 DS likeDS(prgBars) based(progBar2Obj)
D progBar3 DS likeDS(prgBars) based(progBar3Obj)
/free
progBar1Obj = createProgressBar();
if progBar1Obj <> *Null;
progBar1.WindowTop = 3;
progBar1.WindowTitle = 'Test # 1';
progBar1.Color = COLOR_BLUE;
progBar1.ShowPercent = *ON;
endif;
progBar3Obj = createProgressBar();
if progBar3Obj <> *Null;
progBar3.WindowTop = 17;
progBar3.WindowTitle = 'Window 3';
progBar3.Color = COLOR_RED;
progBar3.ShowPercent = *ON;
endif;
progBar2Obj = createProgressBar();
if progBar2Obj <> *Null;
progBar2.WindowTop = 10;
progBar2.WindowTitle = 'Test # 2';
progBar2.Color = COLOR_PINK;
progBar2.ShowPercent = *OFF;
endif;
for x = 1 to 25;
progBar1.Percent = x;
pctA = %editw(progBar1.Percent: ' 0%');
progBar1.StatusMessage = 'This process is at '+ pctA;
displayProgressBar(progBar1Obj);
progBar2.Percent = x*4;
pctA = %editw(progBar2.Percent: ' 0%');
progBar2.StatusMessage = 'Saving changed objects '+pctA+' Complete.';
displayProgressBar(progBar2Obj);
progBar3.Percent = x*3;
pctA = %editw(x:' ');
progBar3.StatusMessage = 'Generic Status Message # ' + pctA;
displayProgressBar(progBar3Obj);
endfor;
destroyProgressBar(progBar1Obj);
destroyProgressBar(progBar2Obj);
destroyProgressBar(progBar3Obj);
exfmt tests01;
*INLR = *ON;
Return;
Note that this example actually creates multiple progress bars concurrently. We define both a pointer and a data structure associated with that pointer for each progress bar created.
As described earlier, we first create the bar using createProgressBar and then identify the associated attributes for that bar, including the color, title, and top position along with the indicator identifying whether or not to show the percentage. Next, we simulate the data processing using a for/endfor loop and incrementing the percent value for each bar along with the status message for each prior to executing the displayProgressBar subprocedure to display each progress bar window. Once all processing is complete, destroy each bar using destroyProgressBar. Upon execution of the final destroyProgressBar, the application will automatically close the display file and set on *INLR. Our sample application then does an exfmt to output its own display file simply to wait for the user to press Enter before exiting. Figure 1 below illustrates what the final screen looks like prior to pressing the Enter key.
Figure 1: Here, you see the output from the PROGBAR service program.
Note that each bar is updated independent of the other two. The application can support a maximum of 10 active progress bars at any given time. Also note that in this example I've associated the PROGBAR service program with a binding directory by the same name to simplify compiles of any programs utilizing this functionality. To compile the service program, use the following two commands:
CRTRPGMOD MODULE(mylib/PROGBAR) SRCFILE(mylib/QRPGLESRC) SRCMBR(PROGBAR)
CRTSRVPGM SRVPGM(mylib/PROGBAR) EXPORT(*ALL)
Once you've added this service program to the PROGBAR binding directory (or the appropriate binding directory of your choice), the sample program (TESTPRGBAR) can be compiled using the standard CRTBNDRPG command shown below:
CRTBNDRPG PGM(mylib/TESTPRGBAR) SRCFILE(MFDEV/QRPGLESRC) SRCMBR(TESTPRGBAR)
Note that the binding directory containing the PROGBAR service program should be in your library list at compile time.
Are We There Yet?
Using a routine like this can help to curb user frustration during long-running interactive jobs by giving them feedback that indicates how much longer it should take. So maybe you won't have to feel like the tortured parent with the kids in the back seat repeatedly asking, "Are we there yet? Are we there yet?"
LATEST COMMENTS
MC Press Online