Goal: I've got some code (TProcessor.Process) that takes a while to run, and
I want a modal form with a progress bar (ProgressForm) to show the progress
of this task.
My basic design is to run this method in a thread, i.e.:
A thread class (TProcessThread) takes a reference to this object, and in
it's Execute method simply calls the long taking method
TProcessor.Process interacts with a TGlobalData class and mainly calls it's
Step method. GlobalData fires an event when it's data changes. This event is
handled by the TProcessThread and calls a method (via Synchronize) that
updates the ProgressBar on the ProgressForm.
The whole thing is started by ProgressForm.RunTask which:
Creates the thread (TProcessThread) and then calls ShowModal.
The thread's OnTerminate event includes code to close the form.
This works fine in simple cases, but in my application that includes more
complex processing in the "process" method, it sometimes hangs. I have
posted a sample application in attachments, which works fine, but I'm hoping
that someone with more experience can spot the weakness of this scheme, that
results in problems in more complex cases.
If I step through the code (and therefore the form in not shown) the problem
(mostly) doesn't occur. But if I run normally then it does.
VIEWS ON THIS POST
Tuesday 9th October 2012