Use Progress Bars When Loading Large Data Files (InstantHotstrings Feature)

Long Load Times Make Us Wonder If Our Computer Has Crashed—Progress Bars Help Us Stay Patient

In the blog, Use the FileSelectFile Command to Save Instant Hotstrings to an AutoHotkey File, I discussed how the InstantHotstring.ahk script saves a set of newly created Hotstrings to a data file. The flip side of the coin reads those same (or any other) Hotstring data files into the InstantHotstring.ahk script, (Loading Hotstrings into the InstantHotstring.ahk Script from Any AutoHotkey (.ahk) File). The solution to reading Hotstring files introduced the problem of the long load times for large files (Timing Script Speed).

Code Slowing Down AutoHotkey Scripts

Library BenefitsTo deal with the loading time issue, I added both the timing technique (linked above) and a Progress command window when loading Hotstrings from a file into the  InstantHotstring.ahk app. (The Progress control window acts as a visual cue for the user while the process works toward completion.) The first step I took to reduce the file loading time involved hiding the GUI window. However, the initial positive results lead me to an erroneous conclusion. But first, let’s take a look at how to set up a Progress GUI control.

Using a Progress Bar

Most often the AutoHotkey Progress GUI control acts as a visual status window when running a particularly time-consuming routine. These slowdowns might occur when running long loops, doing extensive searches, or making numerous hard drive reads and writes. Ideally, the Progress Bar window displays 100% of the task in the window, then incrementing the growing status bar proportionally while working toward completion.

To make it as accurate as possible, we must use an appropriate range for zero to 100%—incrementing the status bar at each step of the process. In this case, the number of lines in the Hotstring file gives us a good approximation of the file length and the total number of steps to completion of file loading.

InstantHotstring Progress Bar

The Progress Bar window used in the InstantHotstring.ahk script (shown at left) displays an incrementing status bar during the file loading process while counting up to the total number of lines in the file.

Counting Lines in a File

The Progress GUI window displayed while running the LoadHotstrings subroutine in the InstantHotstring.ahk script counts up to the number of lines in the named file. To establish an accurate Progress command range, we capture the number of lines in the file. After loading the file into the variable AddHotstring, we use the StrReplace() function to count the number of new lines (`n) in the variable:

StrReplace(AddHotstring, "`n" , "`n", OutputVarCount)

As a trick, the act of replacing every newline character with the same newline character appears a little awkward. However, considering the absence of a character counting function in AutoHotkey, the StrReplace() function does the job.

Note: You can do the same thing with the RegExReplace() function but the AutoHotkey online documentation states, “To replace simple substrings, use StringReplace because it is faster than RegExReplace().”

Creating the Progress GUI Control

Using the line count extracted above as the upper limited in the R option, the Progress command sets up the initial control:

Progress, R0-%OutputVarCount%,%OutputVarCount% lines in file
     ,Loading Hotstrings • • • ,%OpenFile%

The Progress command uses the range option R0-%OutputVarCount% (zero to the number of newlines found in the file), the SubText (below the bar) %OutputVarCount% lines in file, the MainText (above the bar) Loading Hotstrings • • •, and WinTitle (window name) %OpenFile% from the FileRead command (discussed in Loading Hotstrings into the InstantHotstring.ahk Script from Any AutoHotkey (.ahk) File) which loaded the file into the variable AddHotstring.

Updating the Progress Window

Within the loop, we animate the Progress window by incrementing the bar one line at a time:

Progress, %A_Index% ,%A_Index% of %OutputVarCount% lines in file

If the Progress window already exists, the first parameter designates the value of the growing bar or one of three other actions (Hide, Show, or Off). Otherwise, the first parameter contains the options for a newly created Progress window.

Deleting the Progress Window

After completing the series of actions, remove the bar with this Progress command:

Progress, Off

By adjusting these commands and parameters, you can adapt this AutoHotkey Progress command for most actions which might take a little time to complete.

Progress Bars and Script Speed

While you might see a slight slowdown when you add a Progress Bar to your script, in most cases, it does not appear significant. In the InstantHotstring.ahk script, the biggest slowdown originates in the updating (deleting old and adding new) Hotstrings routine for the DropDownList GUI control. But why did I get so much better performance when I hid the GUI window? I’ll write a short blog on the time problem caused by the DropDownList GUI control.

Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)


This post was proofread by Grammarly
(Any other mistakes are all mine.)

(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)


One thought on “Use Progress Bars When Loading Large Data Files (InstantHotstrings Feature)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s