Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Thank you for replying,

 

let's see...

 

On the load event of my form i have something like this:

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:

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

Posted
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
  • Leaders
Posted
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.
[sIGPIC]e[/sIGPIC]
Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...