Brian Andersen
Brian Andersen

Reputation:

How to avoid a thread freezing when Main Application is Busy

I'm having a bit of a problem. I want to display a progress form that just shows an animation on a when the main application preforms heavy operations.

I've done this in a thread and it works fine when the user isn't preforming any operations. But it just stops when my main application is busy.

I'm not able to put Application.ProcessMessages in between the different lines of code because I'm using 3rdparty components with heavy processing time.

My idea was to create a new process and in the process create a thread that execures the animation. Now that wouldn't stop the thread form executing when the main application performs heavy operations.

But as I see it you can only create a new process if you executes a new program.

Does any one have a solution on how to make a thread continue executing even when the main application is busy?

/Brian

Upvotes: 1

Views: 5573

Answers (3)

mghie
mghie

Reputation: 32334

If your worker thread does not have a lower priority than the main thread, you don't use the Synchronize() method, don't call SendMessage() and don't try to acquire any synchronization object that the main GUI thread has already acquired, then your secondary thread should continue to work.

As the VCL isn't thread-safe people do often advise to use Synchronize() to execute code to update VCL controls synchronously in the context of the VCL thread. This however does not work if the VCL thread is itself busy. Your worker thread will block until the main thread continues to process messages.

Your application design is unfortunate, anyway. You should perform all lengthy operations in worker threads, and keep the main thread responsive for user interaction. Even with the fancy animation your app will appear hung to the user since it won't redraw while the VCL thread is busy doing other things and processes no messages. Try to put your lengthy code in worker threads and perform your animation in timer events in the main thread.

Upvotes: 9

Ken White
Ken White

Reputation: 125620

Your logic is backward. Your thread should be doing the "heavy work", and passing messages to your main application to update the progress or animation.

If you leave all the "heavy work" in your main application, the other thread won't get enough chances to execute, which means it won't get a chance to update anything. Besides, all access to the GUI (VCL controls) must happen in the application's main thread; the VCL isn't thread-safe. (Neither is Windows itself, when it comes to visual controls.)

Upvotes: 7

Eugeniu Torica
Eugeniu Torica

Reputation: 7574

If by "Does any one have a solution on how to make a thread continue executing even when the main application is busy?" you mean that main thread is busy you should move the code that is consumming main thread to another other thread. In other words main thread should be responsible for starting and stopping actions and not executing them. Disclaymer: Actually I don't know delphy but I think/hope the concepts are quite similar to C++ or C#.

Upvotes: 0

Related Questions