CPU Usage Mystery

FunUsePro

Newcomer
Joined
Jul 12, 2006
Messages
11
Hello everyone,

For my first post (woot!) I need a bit of input in a strange problem I'm having.

Ok here's the story, I'm writting a program who's interface is a picture animation. So at load time I store the frames of said animation on an array of a structure Frame. Frame contains a single bitmap (a frame, lol) and a region corresponding to that image.

Once the program finishes loading that, it starts a thread that continuously loops thru all the frames, updating the form's background image and its region (has a Thread.Sleep(20), before starting animation again).

works great, smooth animation.

Here's the thing, when I check the Performance tab under Task Manager, my CPU Usage is at 70-100% (1.5 ghz comp). I checked memory and it seems stable, no memory leaks.

Ok so here comes the mystery; sometimes, with my program running, the cpu usage is at 0-4% while other times it's at 70-100%. On both situations the only thing i'm running is my program.

Anyone know why this is happening?
 
Is the background thread updating the user interface directly? If so you will need to Invoke the update routine from the main UI thread.

Also if you use a tool like Process Explorer is the high cpu usage from a single thread or is it fairly even over all threads? Also check how much time is spent in the GC and check if you are getting a high number of collections.
 
Does it slow down other processes and threads? If you use another application while the CPU usage jumps up what happens?
 
Thank you for replying,

let's see...

On the load event of my form i have something like this:
Code:
Thread t = new Thread(new ThreadStart(x));
t.Start();

Now since in function x i'm calling "this.region = y" i have to use a delegate so i'm using this:
Code:
if (this.InvokeRequired)
            {
                getFrameDelegate del = new getFrameDelegate(getFrame);
                object[] Params = new object[1];
                Params[0] = frame;
                this.Invoke(del, Params);
            }
            else
            {
                    this.BackgroundImage = aFrames[frame].bmp;
                    this.Width = this.BackgroundImage.Width;
                    this.Height = this.BackgroundImage.Height;
                    this.Region = aFrames[frame].region.Clone();
                Application.DoEvents();
            }

When I use other applications with my program running... not sure what happens, the performace sometimes spikes down to 50% for a second and then goes bak to 70%sh. Wut i did notice is that other applications do seem to lag.

thanks
 
an update on the subject, i was accidentally creating a new Random class in every iteraion of a loop. took it out and cpu seems to be down to 40sh, still a lot but way less
 
Perhaps you should research some .Net optimization techniques. Then you can go back and look at your code and try to find bottlenecks, sort out redundant and unnecessary code, and re-examine the logic to see just how logical it is. Draw some program flow diagrams and see if things can be organized better, and so on. Re-examining your program's architecture might also help you identify bugs and bottlenecks.
 
That's an excellent idea, thank you.

Already today I changed random.NextInt(0, 100) to random.NextDouble() * 10, .5 second speed improvement! (about...)

this cpu mystery might just be a bunch of lil things that have added up
 
Back
Top