Solved

TOO fast! How to slow execution.

Posted on 2004-10-23
315 Views
Last Modified: 2008-03-17
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?

Thanks
0
Question by:volking
    14 Comments
     
    LVL 8

    Expert Comment

    by:Razzie_
    Well you can halt program execution by using:

    Thread.Sleep(n);

    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.

    HTH,

    Razzie
    0
     
    LVL 38

    Expert Comment

    by:PaulHews
    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.

    0
     
    LVL 8

    Expert Comment

    by:Razzie_
    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! :)
    0
     
    LVL 5

    Author Comment

    by:volking
    @ 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 .........???
    0
     
    LVL 5

    Author Comment

    by:volking
    @ 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.
    0
     
    LVL 38

    Expert Comment

    by:PaulHews
    You trap exceptions by wrapping the code in a Try ... Catch block.  How you deal with the error depends.  Something like this:

    try
    {
       ...   // code that causes error
    }
    catch (Exception ex)
    {
      debug.writeline(ex.tostring);
    }

    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...
    0
     
    LVL 8

    Expert Comment

    by:Razzie_
    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.
    So:

    a) try-catch
    b) yes
    0
     
    LVL 38

    Accepted Solution

    by:
    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.
    0
     
    LVL 5

    Author Comment

    by:volking
    Timers are not my Forte ... I don't have a "vision" of how they work.

    Here is full project souce code (348kb)
    http://www.qpop.com/rsp.zip

    Extract to   C:\
    Will create C:\RokSizPap\.......
    Use Visual Studioi & open:  C:\RokSizPap\RokSizPap.sln
    Run
    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 www.TheCodeProject.com - Discovered can only use One-At-A-Time ... they interfere with each other!!!!!

    Can you help?
    0
     
    LVL 5

    Author Comment

    by:volking
    Phone # in profile
    Only check email every couple of hours (using dial-up)
    0
     
    LVL 38

    Expert Comment

    by:PaulHews
    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.

    0
     
    LVL 38

    Expert Comment

    by:PaulHews
    volking, thanks for the points.  Were you able to get it working with your timer?  
    0
     
    LVL 5

    Author Comment

    by:volking
    @ 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 ...
    0
     
    LVL 38

    Expert Comment

    by:PaulHews
    Glad to hear it is working out then.  :)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Anonabox PRO Tor & VPN Router

    PRO is the most advanced way to fortify your privacy and online anonymity by layering the Tor network with VPN services. Use both together or separately, and without needing to download software onto your devices.

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    Want to pick and choose which updates you receive? Feel free to check out this quick video on how to manage your email notifications.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    909 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now