home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!news.acns.nwu.edu!network.ucsd.edu!ucsbcsl!mcl!scott
- From: scott@mcl.ucsb.edu (Scott Bronson)
- Newsgroups: comp.sys.mac.programmer
- Subject: Re: Creating background tasks (HELP!).
- Message-ID: <scott.714617437@mcl>
- Date: 24 Aug 92 00:50:37 GMT
- References: <1992Aug13.170029.3055@qiclab.scn.rain.com> <1992Aug13.200535.18236@sunfs3.Camex.COM>
- Sender: root@ucsbcsl.ucsb.edu
- Lines: 63
-
- In <1992Aug13.200535.18236@sunfs3.Camex.COM> kent@sunfs3.Camex.COM (Kent Borg) writes:
-
- >In article <1992Aug13.170029.3055@qiclab.scn.rain.com> baer@qiclab.scn.rain.com (Ken Baer) writes:
-
- >>What I want to do is allow our 3D rendering software to render in the
- >>background to the user can use the modeller etc. while rendering.
- >>Could someone either post, or point me to some documented code to set
- >>this up?
-
- >1) When your code gets CPU time (you will know this because it is
- >executing) do your thing.
-
- >2) When the user decides to say something to your running code--for
- >example canceling the rendering he mistakenly started--notice the
- >"Stop" menu choice, or "Cancel" button, or command-period keystroke,
- >and act on it (by stopping in this case).
-
- 3) Set the "accept background null events" in your size resource or you'll
- wait a really long time for your code to finish if you switch it to the
- background... I forget to do this way too often.
-
- >Because this is a user-oriented machine which is valuable precisely
- >for its user interface, be very responsive about noticing that the
- >user is talking to you. Don't make the user pound on the keyboard.
-
- Er, technically speaking the user cannot interact with your program
- while it is in the background. He/she/it must switch it to the
- foreground to cancel the rendering. I assume your renderer already
- allows cancel, so no changes here. I believe the only two events you
- get while you are backgrounding are nullEvt and updateEvt and an
- OS activate event when you get switched back forward. (I think...)
-
- Pass very small values to the sleepTime parameter in WaitNextEvent
- (don't use GetNextEvent unless you HAVE to), and when you get time,
- take a tick or two and get a few more calculations again. Then call
- WaitNextEvent so that other processes can get their fair time. You
- can use the following loop so that you can get the processor for a
- fair amount of time:
-
- // Uncompiled C code from memory below -- use at own
- // risk, but you get the idea.
- long l; EventRecord e;
- while( task not done ) {
- if( WaitNextEvent( everyEvent, &e, 0L, nil ) ) {
- HandleUpdateOrOSEvent();
- } else {
- l = Ticks + 2;
- do {
- SmallIncrementalPartOfYourTask()
- } while( Ticks < l );
- }
- }
-
- Note that if you want to be future compatible, use TickCount() instead
- of the low memory global Ticks. I use TickCount() because I figure that
- if Ticks ever fails, it will be trivial to re-compile using TickCount().
- However, none of the apps I've developed have been commercial (so far)
- so I haven't had to worry about expensive upgrades.
-
- This is how I've been doing it. If anyone has any suggestions, please
- do tell.
-
- - Scott
-