Solved

Design advice wanted regarding Timers, Threads and Graphics

Posted on 2003-12-11
3
180 Views
Last Modified: 2013-11-20
Hello,

I'm maintaing an MFC application which has the following characteristics:

 -It  reads information from the Serial port once a second and displays it
 - it has a huge OnPaint method. which displays hundreds of bitmaps.


Performance is understandably very bad.  I'm assuming it's because the current code calls the MFC OnTimer() function to read the data from the serial port, and then calling the huge OnPaint function, which never completes as it is pre-empted.


I had thought about using threads to prevent this from happening and I need some general advice. Would it be better to put the OnPaint function in a separate thread, or to stop using Timers and put the code that reads from the Serial port in a separate thread?  If I do one or both of these things, what specific idiosyncracies does MFC present?  

I'm aware the UI already runs in a separate thread but beyond that I've never done any multiprocessing under Windows.  
0
Comment
Question by:jeslawson
  • 2
3 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
What does it need to call the OnPaint for?
If only a bit of the client area needs repainting then just repaint that section (check what area has been invalidated).
Maybe keep a DC in memory (needs some work to cope with changing size of window and possible display resolution/colour depth changes) and draw the bitmaps there and BitBlt that to the screen DC during the OnPaint.


In your OnTimer - read the port and invalidate the section of the client area that needs repainting.  Don't try to force it to repaint there and then, let the system do it.

0
 

Author Comment

by:jeslawson
Comment Utility
I'm already using a Memory DC to implement double buffering but at the minute the OnPaint method redraws all the bitmaps, even the ones that aren't within the client area at the time.  I think this is where the bottleneck is.  Agreed?
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 50 total points
Comment Utility
It is a possibility.  Check which part of the display area is invalidated and just renew/BitBlt that area afresh.

In general be careful with assuming a piece of code is slow.  I once spent ages trying to optimise something that it turned out was only a tiny fraction of the total processing time.  Needless to say it didn't improve performance that much - a re-organise elsewhere resulted in a much more responsive app.  In other words really try to identify the slow code.  (GetTickCount can be useful for a crude go, the profiler tool is much more complex but accurate).
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now