I have written a screensaver application in C# .NET and have noticed
that whenever the animation loop is running - CPU usage always spikes
up close to 100%. My application uses a paint thread which is set at
100ms.
the program looks sort of like this:
// constructor
public Form1()
{
InitializeComponent();
// initialization code
tmrPaintRefresh=new System.Timers.Timer(100);
tmrPaintRefresh.Elapsed+=new
System.Timers.ElapsedEventHandler(tmrPaintRefresh_ Elapsed);
}
private void tmrPaintRefresh_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
// update animation variables
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
// contains only code to draw objects on the screen
}
The program works fine and everything looks as it should but CPU usage
is abnormally high.
my question is: where should I be calling Application.DoEvents();?
how can I restructure it so that it is not so CPU intensive. All it is
doing is animating text on the screen; it should not be such a CPU
hog.
Thanks much,
wushupork