Link to home
Start Free TrialLog in
Avatar of burly411
burly411

asked on

Animation during High CPU Load

Hello,

My current program creates an extremely high CPU load while it is loading. During its loading procedure I have a small animation, but unfortunately due to the high CPU load the animation does about 2 frames and then 'hangs' until the program has finished its load procedure.

Is there any way to force the animation to continue? I have tried using "this.Refresh()" during the for loops but that does not help. Adding a breakpoint inside the timer I see that the counter managing the animation does not increment until after the loading procedure.

Can I somehow limit the amount of Processor usage my program will take? Or Is there any way to force this animation to change frames?

Thanks!
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Put the following in during the for loops.

Application.DoEvents()

This should do the trick.
Include the following

using System.Windows.Forms
Whew, now that's a great question that even Application.DoEvents won't help.  I came up with an AnimatedGIF class that managed changing the frames which worked pretty well, but sadly I can't find it anymore.

Bob
ASKER CERTIFIED SOLUTION
Avatar of BurntSky
BurntSky

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of burly411
burly411

ASKER

Multithreading is what I needed to do.

I'll give a quick explanation of what I did.

Normally I start a timer to handle the animation and then I call the reLoad() function.
Now I have put the reLoad() function on a separate thread as follows:

First add:
using System.Threading;


then in the code I declare a private thread variable for the entire form:
private Thread reloadThread;    


on my frm_Load event I have the following:

timAnimStart.Start();    // start the animation timer

this.reLoadThread = new Thread(new ThreadStart(this.reLoad));     // initialize the thread
this.reLoadThread.Start();  // start the thread


So thanks BurntSky it works now. It's not perfect but it works great. Multithreading is my new best friend.