Real Software Forums
http://forums.realsoftware.com/

What's the Best Way to Pause a Program?
http://forums.realsoftware.com/viewtopic.php?f=4&t=28337
Page 1 of 1

Author:  barrytraver [ Tue Jun 02, 2009 5:06 am ]
Post subject:  What's the Best Way to Pause a Program?

I don't mean an indefinite pause, as when you leave the room and want to resume a game after you "pause" and come back.

I mean a short (five-second?) pause (1) to provide the illusion that the computer is taking a moment to figure out its response and (2) to give the human user opportunity to read what is on the screen before it changes.

Example:

I'm finishing a game that allows four options: (1) computer vs. computer, (2) human vs. computer (i.e., human starts first), (3) computer vs. human (i.e., computer starts first), and (4) human vs. human. Suppose we have (pseudo-) code for the computer's move, using option (3):

StaticText1.Text = "I move my piece at " + Str(Row1) + ", " + Str(Col1) _
" to " + Str(Row2) + ", " + Str(Col2) + "."
ComputerMove Row1, Col1, Row2, Col2
StaticText1.Text = "Now it's your turn!"

Suppose further that I want to pause the computer for five seconds between these steps, something like this:

StaticText1.Text = "I move my piece at " + Str(Row1) + ", " + Str(Col1) _
" to " + Str(Row2) + ", " + Str(Col2) + "."
Pause (5)
ComputerMove Row1, Col1, Row2, Col2
Pause (5)
StaticText1.Text = "Now it's your turn!"

(It's not a very good example, but you get the point.)

What's the best way to pause the game for, say, five seconds?

Barry Traver

P.S. The question isn't theoretical. I'm almost finished with a 2D Sprite version of a game I call "NimRow." taken, I think, from Martin Gardner's "Mathematical Recreations" column in Scientific American, although I can't locate the particular column at the moment.

Author:  pentium10 [ Tue Jun 02, 2009 5:14 am ]
Post subject:  Re: What's the Best Way to Pause a Program?

If you are having your code in a Thread, you can call Thread1.Sleep(5000)
Or place your code in a Timer's Action event, setup the timer to fire once in 5000 miliseconds, and once you want to start it, you enable it.

If you are not using Threads or Timers, you should start using them. By having code running in separate thread, your GUI will be fresh and won't freeze, while you have some calculations done behind. Timer's also run in a new thread. Timers are more easier to handle.

Author:  timhare [ Tue Jun 02, 2009 11:48 am ]
Post subject:  Re: What's the Best Way to Pause a Program?

Quote:
Timer's also run in a new thread.

They do not. Timers run in the main thread. The code in a timer can easily tie up the GUI if it takes too long.

Author:  barrytraver [ Thu Jun 04, 2009 6:32 am ]
Post subject:  Re: What's the Best Way to Pause a Program?

Is there any reason why this "standard approach" should not be used?

The following is from Matt Neuberg's REALbasic: The Definitive Guide (2nd edition), pp. 548-549:

Quote:
You can use [Ticks and Microseconds], for example, to pause for a certain interval. You may recall that in chapter 5 these lines were used to pause a racing ProcessBar for some random amount of time:
max = rnd * 15 + 1
i = ticks
while ticks - i < max
wend
That's quite a standard approach for pausing. We define how many ticks we want to pause for. Then we learn the current Ticks value, but we don't really care what it is: we only want to keep checking the Ticks value, as the time goes by, until it has increased by the amount of time we want to pause for. Thus, we are guaranteed that we exit the While loop that much later than we entered it.

Since, according to the REALbasic Language Reference, a tick is a "60th of a second", the following, according to Neuberg, would be "quite a standard approach for pausing" for five seconds (unless things have changed since the second edition of his book was published, although the code seems to work with no problems and the current REALbasic Language Reference doesn't seem to advise against it):

i = ticks
while ticks - i < 5 * 60
wend

Any comments?

Barry Traver

Author:  timhare [ Thu Jun 04, 2009 9:34 am ]
Post subject:  Re: What's the Best Way to Pause a Program?

Quote:
Is there any reason why this "standard approach" should not be used?

I'm not sure I agree that that is "standard". It certainly is "old school", but there are a couple of problems with it. It can cause your app to appear to be locked up, and it burns cpu unnecessarily. The equivalent approach would be to sleep for that time period. It uses less cpu, but still locks the app, unless you're using threads. Bottom line, though, it's best not to do anything that will cause your app to appear "dead". "Thinking" should not be the same as "locked up".

Tim

Author:  piMaster [ Thu Jun 04, 2009 11:42 am ]
Post subject:  Re: What's the Best Way to Pause a Program?

Couldn't you use App.SleepCurrentThread?

Author:  barrytraver [ Fri Jun 05, 2009 2:46 am ]
Post subject:  Re: What's the Best Way to Pause a Program?

Tim,

Good explanation!

"piMaster,"

Good question!

Everyone,

Another question: Would putting App.DoEvents inside the loop in Neuberg's code solve the problem(s) in his "old school" approach?

Barry Traver

Author:  computerfreaker [ Wed Jun 10, 2009 2:10 pm ]
Post subject:  Re: What's the Best Way to Pause a Program?

App.DoEvents isn't usually recommended in GUI apps, IIRC...
Trust me on this: Threads are better for you & for the user's CPU... :)

Author:  realguru [ Wed Jul 08, 2009 2:00 pm ]
Post subject:  Re: What's the Best Way to Pause a Program?

DoEvents is very dangerous (as is the equivalent in Visual Basic) since it lets the user do thing like click buttons that launch code like the code that's calling DoEvents (what, your code isn't re-entrant? have fun debugging).

Pausing a game is probably best done by explicitly handling game time (e.g. via threads) separately from real time and having objects behave based on game time. E.g. imagine you have an in-game "clock" which updates game time before and after every screen refresh. If it's paused, then game time stops updating.

There are MANY reasons to use such approach -- elegantly handling "pause" is merely one of them. Networked games need to be acutely aware of what "time" it is on the client, server, etc. Explicitly handling game time also lets you trivially implement stuff like "bullet time" or "fast forward" in strategy games (e.g. speed things up until something interesting happens). It also lets you (sensibly) keep the UI alive while the game is paused (e.g. in Baldur's Gate, et al, you can pause, and then scroll around the map and issue commands to your dudes).

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/