views:

155

answers:

2

I have a custom control in C#. I have noticed that calling Refresh is much slower than I would like (about 0.1ms), even when I have an empty RePaint function. Basically, my application processes a grid and, one by one, it refreshes each grid cell. This rather inefficient behavior is by design; when enabled I want to be able to actually see what each step of processing has done, and each step only ends up changing one cell. Toggling double-buffering does not make much difference.

Can anyone offer any advice?

Currently, the best improvement I've come up with is to replace my call to Refresh with a call to Refresh2. The latter function is an exact copy of Repaint, except for two lines added to the top, Graphics g = Graphics.FromHwnd(this.Handle); g.Clear(BackColor); and replacing e.Graphics with g. I am suspicious that there is some disadvantage to this, but it does cut the drawing speed in half. Take note that the control I am working with has no subcomponents, so things like validation don't have quite as much concern.

+2  A: 

Refreshing too much would slow it down as you experienced, try to compress events i.e don't refresh when you get a new grid but set a variable when you need to update, and let a timer check that variable every 1 sec for example and refresh then.

cartman
A: 

Is the refresh a automatic recurring event or in response to a user event?

In the first case it should be asynchronous (in its own thread). If in response to a user event, then that would also be true in the case that the "refresh" is not critical to the event.

If the code that updates the interface is too slow, make sure it is only doing the updating of the interface (it should have data already ready.. which is along the vein of double buffering). If that is not the case, do some logging, find out what is taking too much time.

If it must get data first, then some lag is expected. If that is unacceptable, then you'll need to have some caching mechanism. You can have the cache automatically updated asynchronously or have it return what's cached, get fresh data, then cause another update.. or something to that effect.

Greg Ogle
I already did profiling and logging and what not. If I remove the call to refresh or replace it with another call, the time spent goes away. Emptying the rePaint function prevents refreshing from working properly, but does not significantly reduce the time taken. Clearly the refresh function does more than merely call repaint, and the other stuff it does is sucking up time.
Brian