Showing and Hiding Images from within a seperate Thread

I am using Borland C++ Builder 5 and have a form set up with a series of images which I would like to show/hide to give the effect of a moving arrow. The program takes data from a USB interface and depending on the value picks a suitable image to "fill" the arrow up to the required level.

If I Place all the code in the main loop which runs when I press the connect button the program runs fine and refreshes nice and fast (fast enough that it flickering is not really visible). In this configuration however, the program does not check the disconnect button, as it sits in a loop. The only way I have found to exit this program is to use Ctrl-Alt-Del and shut the program down that way (using Windows 98).

In order to try and combat this I implemented a TThread to run the main data logging loop: When the connect button is pressed, the program connects to the USB device and confgures it, after which it starts the logging thread and terminates.

The problem I am having is that the logging now runs unbelievably slowly! If I change it so that it only updates the numbers (in Edit boxes) it works fine, but as soon as I start trying to show images (using a series of 'if' statements) the program drops to a snail's pace. The images flicker continuously, and the arrow is updated less than once a second! At present I 'Hide' all 33 images, 'Show' the one i want and then 'Repaint' that image. If I remove the 'Show' line though, the program returns to a good speed.

Is there any way of speeding this up? Can anyone suggest a better way of doing this?

I donŽt know how bcb5 arranges the main loop. I am using this construction:

Code:

...
while (!done) {
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {
if (msg.message==WM_QUIT) {
done=TRUE;
} else {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
} else {
// Here is where I put parts of the program that need to run "all the time" but must not block the gui
}
}
...

Are you using a double buffer system?
How are you calling for a paint after drawing the image to the backbuffer?
What is in your WM_PAINT handler?

Do you have an intergrated graphics card? Can the PC draw at a reasonable speed?

Also,

PeekMessage() will use 100% CPU as it will return if there is not a msg in the que. (will keep polling the que for a msg for the app)

Try GetMessage() as it will return only when there is a msg in the que. (reducing the CPU usage)

MS has changed the return of GetMessage() and PeekMessage()

while(GetMessage( &msg, (HWND) NULL, 0, 0))

is no longer correct

while(GetMessage(&msg, (HWND) NULL, 0, 0) >0 )

should now be used as a -1 return indicates an error. -1 is non zero so is considered a TRUE response.

Last edited by TechNoFear; July 1st, 2003 at 02:52 AM.

The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

but I think that GetMessage() is a better option for this type of app.

Realy depends on the answers to my other questions.

The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.