PC Review


Reply
Thread Tools Rate Thread

Application.DoEvents and CPU problem

 
 
Arlef
Guest
Posts: n/a
 
      24th Apr 2007
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



 
Reply With Quote
 
 
 
 
Damir
Guest
Posts: n/a
 
      24th Apr 2007
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.




"Arlef" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
>



 
Reply With Quote
 
Alun Harford
Guest
Posts: n/a
 
      24th Apr 2007
Arlef wrote:
> 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
 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      24th Apr 2007
"Damir" <(E-Mail Removed)> wrote in message
news:u$(E-Mail Removed)...
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Application.Doevents problem RSH Microsoft VB .NET 1 13th Jul 2005 09:26 PM
Application.DoEvents() problem Mika M Microsoft VB .NET 0 21st Oct 2004 02:37 PM
NotifyIcon Bug, Application.EnableVisualStyles, Application.DoEvents, and Memory Leaks Michael S. Malley Microsoft C# .NET 0 11th Mar 2004 06:22 PM
NotifyIcon Bug, Application.EnableVisualStyles, Application.DoEvents, and Memory Leaks Michael S. Malley Microsoft Dot NET Framework Forms 0 10th Mar 2004 09:52 PM
NotifyIcon Bug, Application.EnableVisualStyles, Application.DoEvents, and Memory Leaks Michael S. Malley Microsoft Dot NET Framework 0 10th Mar 2004 09:52 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:50 AM.