Re: Idle Time in a loop
Re: Idle Time in a loop
- Subject: Re: Idle Time in a loop
- From: email@hidden
- Date: Mon, 18 Feb 2002 11:14:12 -0800
I have a while loop that is performing a task. How do I allow for time
for, or transfer control to the event loop to process pending events
in the course of the while loop?
It's truly quite easy: you don't. Instead you move your while loop into
a separate thread.
Well, that's one solution. However, it is often much less complex to
avoid the issues multithreading raises, and just return to the top of
the run loop periodically. There's nothing wrong with that, for many
tasks. Basically, request periodic events in some way (you could use an
NSTimer, or schedule performs for yourself using
-performSelector:...afterDelay:..., or schedule performs for yourself in
the run loop using the API in NSRunLoop.h, or use NSEvent's
+startPeriodicEvents... API, or use delayed notifications, or probably
some other ways as well; it depends on exactly how and when you want
your code to run). Each time that you get time from the run loop, do a
little work and then return. Segment your task in such a way that you
remember the work you've already done, and pick up each time where you
left off the previous time.
Background layout of text in AppKit, for example, is done using
delayed notifications set to run when the app is idle. This can be a
better model than using threads, for various reasons.
You could also conceivably run the run loop yourself; see the doc on
-[NSRunLoop run] and other methods. However, as I understand it, this
is generally a very bad idea, although I don't know NSRunLoop well
enough to know why. Can anybody else fill this out?
Ben Haller
Stick Software
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.