Re: Idle Time in a loop
Re: Idle Time in a loop
- Subject: Re: Idle Time in a loop
- From: Eric Peyton <email@hidden>
- Date: Mon, 18 Feb 2002 20:23:28 -0600
I usually do this with a method similar to this (it has some problems of
course but should get you started).
- (void)updateRunLoop
{
id event;
// post events
while (event = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode
dequeue:YES]) {
[NSApp sendEvent:event];
}
return;
}
Call it during idleTimes in your run loop ...
Eric
On Monday, February 18, 2002, at 07:37 PM, Judi Smith wrote:
Ben:
What I am trying to do here is give the interface time to run - you
know process button clicks and the like. I've tried doing running the
loop myself, but that definitely doesn't seem to work. The events are
not processed as you would expect.
The problem is that I need the whole interface - so I can't just pick
particular selectors.
Any clues?
Judi
On Monday, February 18, 2002, at 01:14 PM, email@hidden wrote:
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.
_______________________________________________
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.