ActivityIndicator Stalls

53 views Asked by At

I'm running a lengthy routine in C++Builder that needs an ActivityIndicator to satisfy the user while the routine is running. As part of troubleshooting, I set the AI to Animate = True in the Object Inspector of the IDE so that the AI runs when the app starts. But the AI stalls when I click a button to run the lengthy routine. It begins running again when the routine finishes. Is there a way to make AI run while the routing is running?

1

There are 1 answers

2
Remy Lebeau On

It sounds like you are running the lengthy routine in the context of the main UI thread. If so, then your routine is blocking the main UI thread's message loop from processing UI-related messages, including ones used by the ActivityIndicator.

You should run the lengthy routine in the context of a separate thread instead. You can use TThread or TTask for that purpose. You can create the thread and start the ActivityIndicator, and then have the thread notify the main UI thread when it is finished so you can stop the ActivityIndicator.

If you don't want to run your routine in a separate thread (or can't, ie if it is using something that has thread-affinity to the UI thread), then you can break up the routine into smaller functions that you can queue up asynchronously with TThread.ForceQueue() or equivalent, so that you return control to the main UI message loop in between functions while the routine is running.

If you don't want to do that (ie, because it is too much work to break up the routine's code), then you could simply call Application.ProcessMessages() periodically while the routine is running. But this is generally not advisable to do, as it can cause subtle (or not so subtle) problems in your code if you are not careful with it. A slightly less intrusive approach would be to call the ActivityIndicator's Update() method (and/or the Form's Update() method) instead, so it can process any pending paint messages, at least. But that won't process timer messages used for animation, for instance.

Rule of thumb: NEVER block the UI thread!