-1

Good day ... I am using OnPaint Delphi to create a few lines ... until it's all right ... My problem starts when I try to update these lines ...

I found that to clean the screen, I can use:

Invalidate () 

ex:

Then if True 
Begin 
      Canvas.Ellipse (0,0,100,50); 
End Else Begin 
     Invalidate (); 
end; 

This way works very well ... But the problem is the memory consumption that Invalidate (); uses ....

Normal It consumes less than 0.3% .... Now Invalidate (); it consumes 30% ... is much difference ...

Is there any other function that can be using to clean the screen without using OnPaint both processor ... or a way to make the OnPaint one pass to give Invalidate (); ... Because it is updating all the time, hence consumes lots of CPU .... I need to clean this screen at the time, but others need to update the image of the OnPaint ...

user3185448
  • 81
  • 1
  • 10
  • 1
    Did you place the `Invalidate;` inside the `OnPaint`-Method? – Sir Rufo Feb 08 '14 at 21:06
  • BTW `Invalidate` did **not** clear the screen, it is just to inform the control to repaint as soon as possible. http://docwiki.embarcadero.com/Libraries/en/Vcl.Controls.TControl.Invalidate – Sir Rufo Feb 08 '14 at 21:09
  • @SirRufo That's not what Invalidate does. Repaint or Refresh do that. Invalidate marks the control as dirty by calling InvalidateRect. Only when the message queue is empty will a paint cycle be initiated. – David Heffernan Feb 08 '14 at 21:15
  • @DavidHeffernan Repaint and Refresh do that immediately and Invalidate as soon as possible - yes it is possible, when the message queue is empty :o) – Sir Rufo Feb 08 '14 at 21:22
  • @SirRufo As soon as the message queue has been cleared is accurate. Sorry for being picky. – David Heffernan Feb 08 '14 at 21:23
  • @Sir Rufo That's right ... I am putting inside the OnPaint ... – user3185448 Feb 09 '14 at 16:22

2 Answers2

4

The question is lacking in detail. The code is incomplete. However, it looks like you are calling Invalidate from the same place that you are painting. That is a clear mistake.

Painting works by marking regions of windows as invalid. When a window region is invalid the system will initiate a paint cycle when the message queue becomes empty. At that point it us the program's job to paint itself. When the program needs to change its appearance it invalidates the modified region and then handles the ensuing paint cycle.

It looks like you ate calling Invalidate from your painting method. That leads to an infinitely repeating sequence:

  • You paint.
  • You invalidate.
  • The message queue empties and the window is invalid.
  • The system raises a paint cycle.
  • You paint.
  • You invalidate.
  • And so on and so on.

This infinite sequence of events will lead to you burning CPU cycles.

The solution is to stop invalidating in your paint method. I don't know why you invalidate there, but whatever you are trying to do, invalidating your window is not the answer.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

Thank you ... I think you understood what I was missing ... I did it this way and apparently is working right:

procedure paintestOnclick(blabla);
Begin
    if word= 'Open' then Begin
        onpaintTela := True;
        repaint();
      End;

      if word= 'close' Then Begin
        invalidate();
        onpaintTela := False;
      End;
End;

procedure of the Onpaint:

if onpaintTela then begin
    //Comand paint
End;
user3185448
  • 81
  • 1
  • 10