Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

TOO fast! How to slow execution.

Posted on 2004-10-23
14
Medium Priority
?
360 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
Comment
Question by:volking
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
14 Comments
 
LVL 8

Expert Comment

by:Razzie_
ID: 12388988
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
ID: 12390886
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_
ID: 12392070
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:volking
ID: 12392124
@ 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
ID: 12392131
@ 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
ID: 12392153
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_
ID: 12392157
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:
PaulHews earned 2000 total points
ID: 12392167
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
ID: 12393475
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
ID: 12393488
Phone # in profile
Only check email every couple of hours (using dial-up)
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 12393538
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
ID: 12395280
volking, thanks for the points.  Were you able to get it working with your timer?  
0
 
LVL 5

Author Comment

by:volking
ID: 12399144
@ 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
ID: 12407536
Glad to hear it is working out then.  :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

597 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