I created a steganography(hide text in bitmap) application and I want to add a progress bar to show how long the process works.
procedure TForm1.Button2Click
First, you need to move the code to its own thread. Otherwise the GUI will not respond. You also have to make sure the code is thread-safe.
Anyhow, inside the thread, you need to update the progress bar every now and then. If it so happens that you have an outer loop and an inner loop, where the outer loop iterates once a second or so, you could update the progress bar at that place. If you only have one big loop, you might not want to update the progress bar at every iteration; for instance, a single iteration might perhaps be done in only a few milliseconds.
Instead, you can make sure to update the progress bar once every second, or so. To accomplish this, you can use GetTickCount
:
tc := GetTickCount;
if Terminated then Exit;
if tc - oldtc > 1000 then
begin
PostMessage(FProgressBarHandle, PBM_SETPOS, TheNewPosition, 0);
oldtc := tc;
end;
This also shows a way to update the progress bar -- simply post it a message! You could also define your own message and send it to the main from.