home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / sys / mac / programm / 18721 < prev    next >
Encoding:
Text File  |  1992-11-20  |  2.3 KB  |  60 lines

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