Question

I have two instances of a graphics WinForms control. The graphics is rendered by DirectX and may contain animated content.

I started implementing the continuously-rendered content by:

public override void OnPaint(...)
{
    base.OnPaint(...);

    // Render scene

    this.Invalidate();
}

Besides high CPU usage, this method causes for starvation of invalidation of other controls: One of the controls is rerendered again and again and other controls waiting for the CPU to perform their invalidation request, which is never happen (unless forcing it in some way like window resize).

So I removed 'this.Invalidate()' call and call it only when a refresh is needed. It does solve the problem when no animation is displayed, but if both controls show animated content the problem appears again.

I thought about managing all controls in the application in a centralized manager which will 'schedule' Invalidate calls in round robin. However I'm not happy with the need to know all controls in the application in one centralized location, and I'm not sure if this is the right way to overcome the problem.

Is there a standard way to overcome this problem?

Was it helpful?

Solution

You don't Invalidate during a paint event. That is just going to cause another paint event, again and again and again forever. You want to Invalidate when your data changes, and you can also Invalidate only a region if possible to minimize redraw.

For an animation you will want to call Invalidate based upon your frame rate. You can use a Timer or some other mechanism to do so.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top