Poking an NSRunLoop with an NSMachPort
Poking an NSRunLoop with an NSMachPort
- Subject: Poking an NSRunLoop with an NSMachPort
- From: Dave Dribin <email@hidden>
- Date: Thu, 18 Sep 2008 10:39:22 -0500
Hello,
I'm trying to figure out how to get a blocking call to -[NSRunLoop
runMode:beforeDate:] to return. It seems the best way to do this is
to use a "real" (non-timer) input source to poke the run loop, as
mentioned by Chris Kane in this post:
<http://lists.apple.com/archives/Cocoa-dev/2003/Mar/msg01158.html>
To implement this, I've created a class that adds an NSMachPort to the
run loop and then sends it a message to poke the run loop. I based
this class on code from the "Configuring a Port-Based Input Source"
section of the Threaded Programming Guide [1]. Since I haven't used
NSPorts much, I want to make sure what I'm doing looks proper. A
snippet of code is included.
It all appears to work, but can NSPortMessage's receivePort be set to
nil? The docs aren't clear on this, but I don't care about a getting
replies. I guess I could create a dummy receive port. Does
everything else look sane? Or perhaps there's a better/easier way to
accomplish what I want?
-Dave
[1]: <http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/chapter_6_section_5.htm
>
static const uint32_t kPokeMessage = 100;
@implementation DDRunLoopPoker
- (id)initWithRunLoop:(NSRunLoop *)runLoop;
{
self = [super init];
if (self == nil)
return nil;
_runLoop = [runLoop retain];
_pokerPort = [[NSMachPort port] retain];
[_runLoop addPort:_pokerPort forMode:NSDefaultRunLoopMode];
[_runLoop addPort:_pokerPort forMode:NSModalPanelRunLoopMode];
_pokeMessage = [[NSPortMessage alloc] initWithSendPort:_pokerPort
receivePort:nil
components:nil];
[_pokeMessage setMsgid:kPokeMessage];
return self;
}
- (void)pokeRunLoop;
{
[_pokeMessage sendBeforeDate:[NSDate date]];
}
@end
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden