Board index » cppbuilder » While(1) In Main VCL Thread

While(1) In Main VCL Thread

I have an application which does a lot of processing in the background
threads.  I have noticed though that when I enter the equivalent of a
while(1) loop in the main VCL thread, the background threads suspend as
well.  Why is this?

Thanks,

James A. Wheeler, PE

james_wheeler...@yahoo.com

 

Re:While(1) In Main VCL Thread


PE,

It's because your application only does things in response to messages and
messages are only retrieved by your main thread. If you tie the main thread up
in an extensive processing loop it won't retrieve messages and therefore
there'll be nothing for it to respond to.
There are two solutions.

The simplest is to call Application->ProcessMessages() regularly during the
processing. Don't call it too often though as it takes CPU time away from your
main routine.

The second solution is better but can get out of hand. Perform your processing
in a second thread and let the main thread get on with being a friendly,
responsive application. By "out of hand" I mean that it opens a can of worms
and if you are unlucky or careless you can end up buried in it.

Andrue Cope
[Bicester, UK]

Re:While(1) In Main VCL Thread


Hi, James!

When you do that loop (which you should not do, BTW, as it is not
multitasking-friendly), your VCL process burns CPU cycles, and since the
threads are probably equal priority, but are not always ready to run,
while the loop is, the main thread gets the lions share of the CPU.

The answer is to simply let the application go into it's normal, message
prepped state. It will then consume almost no resources. It will, of
course, in that state, allow message processing (i.e. button presses and
keystrokes) to be processed. If you don't want that, you can have the
TApplication::OnMessage reject all messages until processing is
complete. You will have to write your own handler for that event, of
course, and, in BCB4 and below, you must assign it to the TApplication
object programmatically. BCB5 has a TApplicationEvents component that
can be used instead.

I hope this helps.

------
Mark Cashman, TeamB C++ Builder
http://www.temporaldoorway.com/programming/index.htm
C++ Builder, JBuilder programming information
Home of The C++ Builder Programmer's Webring - Join us!
------

Other Threads