'First thing that will help is to recognize that "as fast as possible" isn't needed. Just "as fast
as is perceptible to the user".'
Ben: that can be pretty difficult to define, since you don't always know how powerful the end user's
computer is.
"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:OF**************@TK2MSFTNGP05.phx.gbl...
lightdoll wrote:
Hello everyone.
i want to know how to decrease the cpu usage in my program.
i have to update quickly many data on gui,
so i tried to decrease the cpu usage on gui but i couldn't solve the
problem.
i found the code with probloem why the cpu usage increased by debug..
Rectangle invalidateRect = new Rectangle(_detailVisibleRect.Left,
_detailVisibleRect.Top + item.Y - _vScrollBar.Value,
_detailRect.Width, item.Height);
this.Invalidate(invalidateRect);
if i don't call this code, then the cpu usage is ok,
but after i call this code, the cpu usage of my problem become
between 10 ~ 15%.
Well, that's what causes the screen to redraw, so I would expect it to use
some CPU. Maybe it doesn't need as much as it currently uses though.
>
could you tell me how to solve this kind of problem if you have to
update many data
on GUI as fast as possible.
First thing that will help is to recognize that "as fast as possible" isn't
needed. Just "as fast as is perceptible to the user". So, for example, you
could limit animation to 30/second or data updates to 5/second and the user
won't notice the delay.
Next, although your code does a great job of defining the update region
correctly, if your paint code doesn't use the update region, there's no
benefit.
You might simply try this:
Add a timer with a period of 150ms.
Change the invalidate code to simply set a boolean dirty flag.
In the timer callback, if the dirty flag is set, invalidate the whole window
and clear the flag. (If you have multiple threads, you should use
Interlocked.Exchange to clear the boolean flag and give you the old value)
This will probably decrease the number of repaints dramatically and
therefore save you a lot of CPU.
>
^^....help me