Animation during High CPU Load


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?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Put the following in during the for loops.


This should do the trick.
Fernando SotoRetiredCommented:
Include the following

using System.Windows.Forms
Bob LearnedCommented:
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.

Are you multi-threading?  I haven't tried doing exactly what you're looking for, but it seems to me that if you were to run your "high CPU load" in a worker thread and then use a callback when it's finished, the animation should continue running on the original thread.

This is a very good question though; perhaps someone has a creative solution.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
burly411Author Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.