Application.DoEvents and CPU problem

A

Arlef

Basically, I have this piece of code and it put my cpu usages to 100%.
However, in I uncomment the sleep line that sleeps the current thread the
CPU tends to be about 50%. I know that this is centred around the
Application.DoEvents() method. Is there a proper way to do this such that
we can eliminate the call to Thread.Sleep(100); ?

Form1 TheForm = new Form1();
TheForm.Show();
while(TheForm.Created)
{
TheForm.TheRosettes.incrementSize();
TheForm.TheRosettes.rotateRosettesRight();
TheForm.TheRosettes.Invalidate();
// System.Threading.Thread.Sleep(100);
Application.DoEvents();
}


Many thanks for your help!

Junior
 
D

Damir

Hallo

There is on my opinion not a really better way, and the CPU is not really
used 100%. Its because "while" command.
You can put it into separate thread, this will probably work much better.
 
A

Alun Harford

Arlef said:
Basically, I have this piece of code and it put my cpu usages to 100%.
However, in I uncomment the sleep line that sleeps the current thread the
CPU tends to be about 50%. I know that this is centred around the
Application.DoEvents() method. Is there a proper way to do this such that
we can eliminate the call to Thread.Sleep(100); ?

Form1 TheForm = new Form1();
TheForm.Show();
while(TheForm.Created)
{
TheForm.TheRosettes.incrementSize();
TheForm.TheRosettes.rotateRosettesRight();
TheForm.TheRosettes.Invalidate();
// System.Threading.Thread.Sleep(100);
Application.DoEvents();
}

This isn't VB. (ie. VB before .NET)
When you make a call to Application.DoEvents() in VB before .NET, the
thread would sleep. This insanity doesn't happen any more.

In your application, it looks like the speed that the graphics moves at
depends on the speed of the CPU. This is usually a bad idea. What you
normally want to do is to call incrementSize and rotateRosettesRight
passing as an argument the length of time since they were last called.
They can then rotate (or whatever) the correct amount, and you can sleep
at the end of the loop if you have time to (because there's no point
calling Invalidate() faster than the refresh rate of the monitor).

Alun Harford
 
P

Peter Duniho

Damir said:
Hallo

There is on my opinion not a really better way, and the CPU is not really
used 100%. Its because "while" command.

I disagree. If the Task Manager shows the CPU usage at 100%, then it likely
is actually at 100%. The code posted basically tries to redraw the graphics
as fast as possible, and in this scenario the only real limit is the CPU, so
CPU usage will naturally be at 100%.
You can put it into separate thread, this will probably work much better.

Moving the code to a different thread won't have any effect at all on
performance (and will, of course, create a cross-thread form access
problem).

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top