Solved

Design advice wanted regarding Timers, Threads and Graphics

Posted on 2003-12-11
3
183 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Folder Comparison 12 54
Execute multiple curl cmds with sleep and send output to file 10 104
Way to decrease size of apk file 9 86
Problem to App 4 97
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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 …
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 video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

856 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