Solved

Design advice wanted regarding Timers, Threads and Graphics

Posted on 2003-12-11
3
182 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
ID: 9919846
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
ID: 9949497
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
ID: 9949586
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

778 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