As you know, all UI code must be executed on the main GUI thread. That's why you are calling Synchronize
to update your GUI. Synchronize works broadly like this:
- The task to be performed on on the main thread is placed in a queue.
- The main thread is signaled to indicate that synchronize tasks are pending.
- The background thread blocks.
- When the main thread next checks to see if there are pending synchronize tasks, it executes them.
- The background thread is signaled to indicate that the task has been executed.
- The background thread stops blocking and continues executing.
It's quite a complex little dance.
Your problem is that your main thread is busy executing some long running task. Presumably in the calls to DoHeavyWork1
and DoHeavyWork2
. And that means that the GUI thread does not perform item 4 in a timely fashion. What's more, the main thread blocks the background thread, somewhat negating the utility of threads.
Your problem, fundamentally, is that your main GUI thread is busy doing something other than servicing the GUI. You should dedicate your GUI thread to servicing the GUI. It should take on nothing else, and certainly not any long running tasks. Once you manage to ship all the non-GUI tasks out of the GUI thread and onto background threads, you'll find that you application is responsive.
Finally, I recommend that you remove that call to Application.ProcessMessages
from UpdateDots
. You probably added it to try to deal with your non-responsive GUI. But it won't help at all because your problem is that UpdateDots
is not executing in a timely fashion.