[comp.sys.mac.programmer] MacApp, backgrounding etc

ralph@computing-maths.cardiff.ac.uk (Ralph Martin) (04/17/89)

I'm getting along quite well writing an application in MacApp, and I
cant seem to find in the 2.0b5 documentation details on how to be
polite under Multifinder. Basically, at one point, when the user hits
a DoIt button, the App needs to do some intense calculations for quite
a long time. During that time, I would like the user to be able to
interact with the Mac, and indeed even switch the App into the
background so he can do something else useful while the crunching is
going on. Can anyone give me some pointers to how to go about this?
Thanks

lsr@Apple.COM (Larry Rosenstein) (04/20/89)

In article <674@cf-cm.UUCP> ralph@computing-maths.cardiff.ac.uk (Ralph 
Martin) writes:
> I'm getting along quite well writing an application in MacApp, and I
> cant seem to find in the 2.0b5 documentation details on how to be
> polite under Multifinder. Basically, at one point, when the user hits
> a DoIt button, the App needs to do some intense calculations for quite
> a long time. During that time, I would like the user to be able to
> interact with the Mac, and indeed even switch the App into the
> background so he can do something else useful while the crunching is

First, you need to make sure that your program is set up to run in the 
background.  This requires setting a bit in the SIZE resource that says it 
canBackground.  In your Rez file, canBackground is the name of the bit.

Second, you need to set up your computation so that it happens during the 
idle time of the application.  The best way to do this is to make a 
subclass of TEvtHandler, and override the DoIdle method.  You will also 
need to set the fIdleFreq field depending on how often you want your task 
to run.  (If you want the task to get as much idle time as possible, then 
you should set this to 0.)

Finally, you need to install and instance of your class into MacApp's 
cohandler chain.  This is a list of TEvetHandler objects that get a chance 
to handle events if you view, document, application, etc. doesn't want to. 
 Cohandlers also get idle time.

When you want to initiate the computation, create an instance of the 
class, and use TApplication.InstallCohandler to install it.  You could 
also do this once, and simply adjust the fIdleFeq depending on whether 
there is anything for the computation to do.  (InstallCohandler can also 
be used to remove the cohandler from the list.)

After you've done this, the DoIdle method will get called periodically if 
there are no othe events pending.  For the best responsiveness, your 
computation should do a small bit of work each time and return.  You can 
save the state of the computation in the object.

(Alternatively, you may be able to call EventAvail to see if any events 
are pending and return at that time; the problem with this is that it 
wouldn't give other event handler objects a chance to do idle processing.)

Let me know if you have other questions.

Larry Rosenstein, Apple Computer, Inc.
Object Specialist

Internet: lsr@Apple.com   UUCP: {nsc, sun}!apple!lsr
AppleLink: Rosenstein1