#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    3
    Rep Power
    0

    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?

    Any help would be very much appreciated!

    P.S. the 'if' statements are arranged as follows:

    if (data < -75)
    {show arrow1}
    if (-75 <= data < -70)
    {show arrow2}
    if (-70 <= data < -65)
    {show arrow3}
    etc...

    Thanks again...
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    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
      }
    }
    ...
    Hope this helps...
  4. #3
  5. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    28
    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.

    Frank Zappa
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,645
    Rep Power
    4248
    You can also try placing this statement:
    Application->ProcessMessages();

    within your main update loop. This will allow it to check if the Disconnect button has been pressed or not. You won't need a separate thread then.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    Originally posted by TechNoFear
    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.
    The 100% CPU is IMHO not an issue here because if it is not polling for messages, it is drawing the arrows. Or do you see any other drawbacks?

    M.
    ps. Thanks for the hint about ">0", IŽll try to remember that
  10. #6
  11. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    28
    >>Is there any way of speeding this up?

    I might have misunderstood the question...........

    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.

    Frank Zappa

IMN logo majestic logo threadwatch logo seochat tools logo