home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sys.mac.programmer
- Path: sparky!uunet!wupost!gumby!kzoo!k044477
- From: k044477@hobbes.kzoo.edu (Jamie R. McCarthy)
- Subject: Re: best way to pause in app ?
- Message-ID: <1992Nov20.185655.28846@hobbes.kzoo.edu>
- Organization: Kalamazoo College
- References: <brownlow.722219238@stmartin> <1992Nov20.164209.2573@den.mmc.com>
- Date: Fri, 20 Nov 1992 18:56:55 GMT
- Lines: 49
-
- weiser@pogo.den.mmc.com (Matt Weiser) writes:
- >brownlow@informix.com (Keith Brownlow) writes:
- >>
- >> Is there a toolbox command to make the application wait x number of seconds
- >> before continuing processing ?
- >
- >PROCEDURE Delay (numTicks: LONGINT; VAR finalTicks: LONGINT);
-
- Help stamp out _Delay!
-
- Or at least, stamp it out if there's a better way to do things. _Delay
- grabs the CPU and doesn't let any other app have any time. If you meet
- two conditions, you can be much friendlier: (1) your delay period is
- longer than two or three ticks, and (2) it's not absolutely 100%
- essential that you make it back right on time.
-
- If those conditions apply--and I think they will, because you implied
- you'll be waiting at least one second, and you're probably not writing
- for a hospital--try code like the following:
-
- void betterDelay(short nTicksToDelay)
- {
- long lastTick = TickCount();
- while (nTicksToDelay > 3) { // the lower this number, the friendlier
- // give time to other apps
- EventRecord myEventRec;
- Boolean needToHandleEvt;
- needToHandleEvt = WaitNextEvent(0, &myEventRec,
- Max(nTicksToDelay - 5, 0),
- NULL);
- nTicksToDelay -= TickCount() - lastTick;
- lastTick += nTicksToDelay;
- }
- while (TickCount()-lastTick < nTicksToDelay) {
- // wait in a tight, unfriendly loop
- }
- }
-
- I know some gurus will be able to improve on this. For example, if your
- app is getting update events and you're not responding to them, then
- no other app will get time anyway. Plus, it's not a good idea to lock
- out suspend/resume events, because if another process brings itself to
- the front, you'll be suspended without your knowledge. But if you don't
- have any update events pending when you call this, you stand a very good
- chance of being excellent to your fellow apps. And a good chance is
- better than none.
- --
- Jamie McCarthy Internet: k044477@kzoo.edu AppleLink: j.mccarthy
- My contribution to urban blues...
-