[comp.sys.mac.programmer] Time Manager -- please help!

spaid@spot.Colorado.EDU (SPAID RICHARD) (12/16/88)

[ I just discovered this group, so forgive me if this is old hat. ]

Has anyone out there used the Time Manager much?  I've tried to use it,
under both LSP and LSC (v3 of the latter), and had very little luck.
Here's my latest attempt:

/* example of Time Manager tasks 
   Adapted from example in Tech Note #180
*/

#include <stdio.h>
#include <TimeMgr.h>

#define	Interval	1000L		/* one second interval */


int Counter;				/* glob. var. that the timed task will access */
EventRecord MyEvent;
TMTask myTimeTask;

pascal void DoTT()	
/* the task installed as a Time Manger task -- just increments Counter
	and returns
*/		  
{
	SetUpA5();			/* need to do this in interrupt service routines */
	Counter++;
	PrimeTime(&myTimeTask, Interval);	/* make it happen over and over */
	RestoreA5();	
}


main()
{
	printf("Time Manager tester\n");
	
	Counter = 0;					

	myTimeTask.tmAddr = (ProcPtr) DoTT;		/* set up the TMTask record */
	InsTime(&myTimeTask);						/* install it */
	PrimeTime(&myTimeTask, Interval);		/* start it */

	do
	{
		printf("%d\n",  Counter);	
	}		
	while (!GetNextEvent(mDownMask,&MyEvent)); 	/* go until a mouse click */

	RmvTime(&myTimeTask);						/* get rid of it */
}


Simple stuff, eh?  Should just print a bunch of numbers that increment every
second, right?  Well, it doesn't work.  Sometimes it will count up to three,
and then keep printing threes for ever, sometimes it will get up to five or
six.  It seems as if the task just doesn't get re-Primed after a few times.
Also LSC beeps twice after I close the parting message that stdio puts up --
what is it trying to tell me?

If anyone can tell me what I'm doing wrong, I would be VERY appreciative.  I
need the Time Manager for both a project at work and projects at home, and
it's been a source of frustration for me for a long time.  Thanks in advance
for any and all suggestions or comments.

(Novice in netland -- hence no signature)

jmpdude@Apple.COM (Mike Puckett) (12/19/88)

In the source-code fragment that you provided, the only potential problem
that I spotted was in your Time Manager task routine.  The routines
SetUpA5() and RestoreA5() will not (potentially) work correctly with the
Time Manager because CurrentA5 may not be pointing to your application's
globals when the Time Manager task gets called.  This is especially true
in a MultiFinder environment.  So, what you need to do is make yourself
a set of routines that will ensure that A5 is set up correctly when
your task is called (this is what I had to do).  The routines I have created
are called, CacheA5(), BeginA5Swap(), and EndA5Swap().  CacheA5() is called
in the main body of my program before a PrimeTime() call is made; what it does
is to store the CurrentA5 value into a KNOWN (very important!) location.  The
jobs of BeginA5Swap() and EndA5Swap() are analogous to SetupA5() and 
RestoreA5(), except that they work with the KNOWN location of CacheA5() instead
of CurrentA5 [See Tech. Notes:  #180 and #208. Also, see the Programmer's
Guide to MultiFinder.]  By the way, before I sent this response, I tried
your idea out [in MPW, using my A5 handler routines], and it works like
I presume you want:  every 1,000 ms (1 second) the current count is printed
until the mouse button is clicked.

Mike Puckett,
jmpdude@apple.com