TOO fast! How to slow execution.

I've written a game. When the game executes it can process literally thousands of "steps" in just seconds.

What's the best approach for slowing execution down such that the game "speed" will be about the same no matter CPU speed?

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.

Well you can halt program execution by using:


where 'n' is the number of ms to halt execution. So Thread.Sleep(1000); would pause the program for 1 second. The Thread class is in the System.Threading namespace.


Use a timer...  You can use System.Windows.Forms.Timer or System.Timers.Timer classes.  Both fire an event a defined intervals.  

Using Sleep is not a great idea.  Your program is not responsive when the thread is sleeping.

Some things don't need to be regulated by time.  It wouldn't make much sense to limit your program's AI (if it has any) by regulating it through a timer.  However, video games with animation should be timer event driven so that the action appears the same on all systems.  The System.Timers.Timer will be slightly more accurate than the winforms version, because it's events are not processed through the form's message queue.

My bad, PaulHews is absolutely right, missed the fact it was a game, thought you just wanted to halt program execution for x seconds.

Go with the Timer! :)
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

volkingAuthor Commented:
@ PaulHews
First, this is a Video Game with many separate things happening.

Second, took your advice and tried a System.Windows.Forms.Timer (needed 8) ... works great ...... BUT ........ hmmmmmmm ...... Now I have a gremlin! ...

Once in awhile (very very seldom with no apparent pattern) the program generates a "Generic GDI+" error. When I look close at the error message (a) I really don't understand what I'm reading and (b) it "appears" a GDI+ call was interupted by a Clock-Tic event. (If I click "Continue" and ignore problem, the program "appears" to continue along happily)

What do you think? How to fix?

a) capture error in code (HOW???) auto ignore and continue program
b) Change to System.Timers.Timer class (Will that solve the problem?)
c) some other advice .........???
volkingAuthor Commented:
@ PaulHews - My first step will be to collapse 8 separate timers into ONE "fine" timer, keep track of 8 timing-schemes (inside the one timer) and fire proper action(s) as needed. I'll let you know if this helps with Gremlin.
You trap exceptions by wrapping the code in a Try ... Catch block.  How you deal with the error depends.  Something like this:

   ...   // code that causes error
catch (Exception ex)

Will at least write the full errror and stack trace to the output window... Useful because you can post the exception here, and analyse it further.  It also won't halt execution, however that may not be a good thing.  Unhandled exceptions caused by problematic graphics code can cause memory leaks.

It might be useful to try the other system timer.  As noted before, it isn't handled by the form's event processing, so that may be an advantage.  Hard for me to say without knowing what your code is actually doing.

>@ PaulHews - My first step will be to collapse 8 separate timers into ONE "fine" timer, keep track of 8 timing-schemes (inside the one timer) and fire proper action(s) as needed. I'll let you know if this helps with Gremlin.

Ouch, yes.  You definitely don't want 8 separate system timers making graphics calls.  Far better to handle it with state variables within a single timer event...
volking: you should not use the System.Windows.Forms.Timer because that is NOT an accurate timer by all means. It is best to use a System.Timers.Timer or a System.Threading.Timer.

I think switching to another timer will also get rid of the GDI+ error.

a) try-catch
b) yes
I think reducing the 8 to a single timer may be an even more important factor.  What may be happening is that one timer is triggering a graphics call before the last one has finished its work.  Thus reduce the code path to a single timer and you shouldn't have that problem.

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
volkingAuthor Commented:
Timers are not my Forte ... I don't have a "vision" of how they work.

Here is full project souce code (348kb)

Extract to   C:\
Will create C:\RokSizPap\.......
Use Visual Studioi & open:  C:\RokSizPap\RokSizPap.sln
Ignore everything on first screen (don't work yet) but press "GAME" button
Game window opens ... screen bottom press button "Phase 1 play"
Be patient ... takes a few rounds to trigger elimination ... first place I'm using Timer.

P.S. Got timer from - Discovered can only use One-At-A-Time ... they interfere with each other!!!!!

Can you help?
volkingAuthor Commented:
Phone # in profile
Only check email every couple of hours (using dial-up)
It's against the member agreement to solve problems by email or phone...And I don't have full VS 2003, I've only got VS 2002, and VB.NET for 2003, so no version of C# that can open that project.  I'll browse the code, but I can't guarantee I'll be able to help much further than I have.

volking, thanks for the points.  Were you able to get it working with your timer?  
volkingAuthor Commented:
@ PaulHews - Nope ... discarded timer from TheCodeProject. Rebuilt from scratch using System.Timers.Timer.
class = TimeCentral
Any procedure anywhere subscribes to TimeCentral.NotifyMeList event
Any procedure anywhere calls TimeCentral.SetTimePoint((notify me every X milliseconds), (until a total of Y milliseconds have passed), (myPostMark is ABC))
One central timer object allows multiple event-subscriptions ...
... where returned event args identify which requestor asked to be notified.
... thus each requestor simply ignores any event not "post-marked" for it.

At first crack .... it seems to be working fine .....

After working with it awhile longer ... I will put it in TheCodeProject for everyone.

Later ...
Glad to hear it is working out then.  :)
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.