home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-11-24 | 5.5 KB | 209 lines | [TEXT/PJMM] |
- program TestPTasks;
-
- { Task Manager -- Background processing support}
- { version 2.2}
-
- { This software source package is Copyright © 1990-91 by Michael Hecht. All Rights}
- { Reserved. It may be freely distributed in source or object code format; however,}
- { the source code may not be sold for profit or charged for in any way. The source}
- { code must be distributed as a package including all H files, sample code and}
- { projects, and documentation.}
-
- { I welcome any comments or suggestions that will help me improve or extend the}
- { functionality of the Task Manager. You can reach me at:}
-
- { Internet: Michael_Hecht@mac.sas.com}
- { AppleLink: SAS.HECHT}
-
- { Happy Tasking!}
-
- { --Michael Hecht}
-
- { Pascal Conversion by Peter N Lewis <peter@ncrpda.curtin.edu.au>, Aug 1992}
-
- uses
- Tasks;
-
- type
- SlaveRecord = record
- portRect: Rect;
- title, msg1, msg2: str31;
- slaveWindow: WindowPtr;
- end;
- SlavePtr = ^SlaveRecord;
- SlaveHandle = ^SlavePtr;
-
- procedure DrawRandomMsg (wp: WindowPtr; msg1, msg2: str31);
- var
- r: Rect;
- scrollRgn: RgnHandle;
- begin
- SetPort(wp);
-
- { Scroll the screen }
- scrollRgn := NewRgn;
- r := wp^.portRect;
- ScrollRect(r, 0, -16, scrollRgn);
- FillRgn(scrollRgn, white);
- DisposeRgn(scrollRgn);
-
- { Pick one of two messages at random and draw it }
- MoveTo(5, 128);
- if odd(random) then
- DrawString(msg1)
- else
- DrawString(msg2);
- end;
-
- procedure SlaveProc (theSlaveHandle: SlaveHandle);
- var
- theSlave: SlaveRecord;
- slaveWindow: WindowPtr;
- oe: OSErr;
- begin
-
- { Our refCon is really a SlaveHandle; get the record it points to {}
- theSlave := theSlaveHandle^^;
-
- { Create a window for our slave }
- slaveWindow := NewWindow(nil, theSlave.portRect, theSlave.title, TRUE, noGrowDocProc, FrontWindow, TRUE, 0);
-
- { Store the window ptr in our SlaveHandle so the TaskTerm procedure can find it }
- theSlaveHandle^^.slaveWindow := slaveWindow;
-
- { Draw messages forever (the main task will stop us when it's time) }
- while true do begin
- { Let other tasks run }
- oe := TaskYield;
-
- { Draw one of our messages }
- DrawRandomMsg(slaveWindow, theSlave.msg1, theSlave.msg2);
- end;
- end;
-
- procedure SlaveTermProc (theSlaveHandle: SlaveHandle);
- begin
- { Time to close our window (if it was ever created) }
- if theSlaveHandle^^.slaveWindow <> nil then
- CloseWindow(theSlaveHandle^^.slaveWindow);
-
- { SlaveHandle no longer needed }
- DisposHandle(handle(theSlaveHandle));
- end;
-
- function NewSlave (bounds: Rect; title, msg1, msg2: str31): OSErr;
- var
- err: OSErr;
- theSlave: SlaveRecord;
- theSlaveHandle: SlaveHandle;
- trn: integer;
-
- begin
- { Create a new slave; first, initialize a SlaveRecord }
- theSlave.portRect := bounds;
- theSlave.title := title;
- theSlave.msg1 := msg1;
- theSlave.msg2 := msg2;
-
- { Be sure to set this to nil, in case the term proc gets called too early! }
- theSlave.slaveWindow := nil;
-
- { * Next; convert it to a SlaveHandle, so it can sit in the heap until the}
- { * slave can retrieve it.}
-
- err := PtrToHand(@theSlave, handle(theSlaveHandle), sizeof(SlaveRecord));
- if err = noErr then begin
- { Create the slave task, using the SlaveHandle as its taskRefCon }
- err := NewTask(@SlaveProc, @SlaveTermProc, longInt(theSlaveHandle), trn);
- if err <> noErr then begin
- DisposHandle(handle(theSlaveHandle));
- end;
- end;
- NewSlave := err;
- end;
-
- var
- err: OSErr;
- masterWindow, theWindow: WindowPtr;
- r: Rect;
- timeToQuit: Boolean;
- theEvent: EventRecord;
-
- begin
- { Initialize the ToolBox }
-
- { Turn on tasking }
- err := InitTasking;
- if err = noErr then begin
- { Make slaves }
- SetRect(r, 261, 45, 507, 176);
- err := NewSlave(r, 'Task 1', 'See my Task 1 message?', 'Of course you do—I’m Task 1!');
-
- SetRect(r, 5, 206, 251, 337);
- if err = noErr then
- err := NewSlave(r, 'Task 2', 'THIS is Task 2?', 'Y E S, it is!');
-
- SetRect(r, 261, 206, 507, 337);
- if err = noErr then
- err := NewSlave(r, 'Task 3', 'The great and mighty Task 3.', 'See me??? I’m Task 3!');
-
- if err = noErr then begin
- { Make master window }
- SetRect(r, 5, 45, 251, 176);
- masterWindow := NewWindow(nil, r, 'Master Task', TRUE, noGrowDocProc, POINTER(-1), TRUE, 0);
-
- { A very simple event loop }
- timeToQuit := FALSE;
- while not timeToQuit do begin
- if WaitNextEvent(everyEvent, theEvent, 0, nil) then begin
-
- case theEvent.what of
-
- mouseDown: begin
- case FindWindow(theEvent.where, theWindow) of
- inGoAway: begin
- { Track it }
- if TrackGoAway(theWindow, theEvent.where) then begin
- { It's time to quit if they close the master window }
- timeToQuit := masterWindow = theWindow;
- end;
- end;
- inContent, inDrag: begin
- { Bring it to the front if need be }
- if theWindow <> FrontWindow then
- SelectWindow(theWindow);
- end;
- otherwise
- ;
- end;
- end;
- updateEvt: begin
- { Clear out any update event }
- theWindow := WindowPtr(theEvent.message);
- BeginUpdate(theWindow);
- EndUpdate(theWindow);
- end;
- otherwise
- ;
- end;
- end;
-
- { Allow tasks to run for five ticks }
- err := RunTasks(5);
-
- { Let the master task do something useful }
- DrawRandomMsg(masterWindow, 'Obey me—I am the Master!', 'Close me to quit.');
-
- { Continue until it's time to quit or there are no more tasks }
- end;
-
- { Close the master window }
- CloseWindow(masterWindow);
-
- { * Don't forget to do this! It will dispose of all tasks,}
- { * thereby calling each one's TaskTerm procedure.}
-
- end;
- err := TermTasking;
- end;
- end.