ProgressBar delayed update


#1

I have a rather heavy task that takes a few seconds for the message thread to carry out so I wanted to put up a progress bar for the user. I don’t want it to be a modal window (like the ThreadWithProgressWindow), instead I’d like it to be built into my UI. Before this heavy task starts I make the progress bar visibile and then carry out the task, while updating the ProgressBar’s variable along the way. But it doesn’t update it until AFTER the heavy task is complete. I searched around the forum and found an OLD post (2007) in which Jules, you posted this anwer:

I looked at the MessageManager class, and that method is no longer available. Is there a replacement method I’m not seeing, that will serve the same purpose? I tried runDispatchLoop and that just froze the program. I also tried runDispatchLoop, followed by stopDispatchLoop, but that too froze the program.


#2

I think the quickest and easiest way to do this would be to just copy the approach used by ThreadWithProgressWindow but instead of showing an alert window just create a custom component with the progress bar (could be just a ProgressBar) and add it to your UI. Keeping the long task off the main thread will mean all your UI will update as per usual, it also means the task can be cancelled.

If you really don’t wan’t to use a background thread and your long task can be split into blocks you could run the blocks on a timer or similar (multiple calls to triggerAsyncUpdate()?) which will allow the message thread to dispatch any pending messages. You can’t call runDispatchLoop as it blocks which is why the call to stopDispatchLoop never gets called.

But seriously, anything that takes a considerable amount of time should be done on a background thread, you never want to make your UI unresponsive as apart from causing all sorts of problems it just creates an incredibly bad user experience.


#3

Thanks dave96. I put the big task on a background thread and it works like a charm.