Design advice wanted regarding Timers, Threads and Graphics

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.  
jeslawsonAsked:
Who is Participating?
 
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
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
 
AndyAinscowFreelance programmer / ConsultantCommented:
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
 
jeslawsonAuthor Commented:
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
All Courses

From novice to tech pro — start learning today.