Not receiving events from spawned Cocoa thread.

Post questions, comments and feedback to our 3Dconnexion Mac Development Team.

Moderator: Moderators

Post Reply
Jim Trankelson
Posts: 4
Joined: Sat Aug 04, 2007 3:29 pm

Not receiving events from spawned Cocoa thread.

Post by Jim Trankelson » Sat Aug 04, 2007 3:41 pm

Hi,

I'm trying to integrate the spacenavigator into my cocoa app, and really need the event handling to be done in another thread. In my app initialization, I set up the thread like this:

[NSThread detachNewThreadSelector:@selector(setupSN:) toTarget:[self retain] withObject:self];

- (void)setupSN:(id)arg
{
[arg spaceNavigatorBootstrap];
}

- (void) spaceNavigatorBootstrap
{
OSErr oserr;

// Make sure the framework is installed
if(InstallConnexionHandlers != NULL) {

// Install message handler and register our client
oserr = InstallConnexionHandlers(SNHandler, 0L, 0L);

fConnexionClientID = RegisterConnexionClient(kConnexionClientWildcard, 0L, kConnexionClientModeTakeOver, kConnexionMaskAll);
}
...
}

All of the handling is copied from the Cocoa SDK example. The problem is that I'm not actually receiving any events when I set things up like this. My callback function isn't getting called. I've read something about using the NSRunLoop somehow, but I'm slightly confused about how to go about setting that up. Can anyone give me a pointer?

Thanks!

-jt

ettore
Moderator
Moderator
Posts: 127
Joined: Wed Mar 14, 2007 5:55 pm
Location: SF Bay Area, CA
Contact:

Post by ettore » Mon Aug 06, 2007 10:37 am

Hi Jim,

is your thread exiting immediately after the spaceNavigatorBootstrap method has completed? That's what it seems like. Please verify it using ThreadViewer.app.

The thread spawned (detached) using detachNewThreadSelector: will exit if you don't set up a loop after the call to the spaceNavigatorBootstrap method.

Have a look at Apple's website for how to setup a run loop, there are good resources (explained better than what I could do here):
http://developer.apple.com/documentatio ... index.html

and also the reference class documentation for NSRunLoop:
http://developer.apple.com/documentatio ... rence.html
ettore pasquini
software engineer
3Dconnexion, inc.

Jim Trankelson
Posts: 4
Joined: Sat Aug 04, 2007 3:29 pm

Post by Jim Trankelson » Tue Aug 07, 2007 3:51 pm

Yes!

Thanks. This helped me immensely.

What I needed to do is create a port and continue to run the thread's run loop.

// Install message handler and register our client
oserr = InstallConnexionHandlers(SNHandler, 0L, 0L);

fConnexionClientID = RegisterConnexionClient(...)

[[NSRunLoop currentRunLoop] addPort:self.portObj forMode:NSDefaultRunLoopMode];

NSDate *endDate = [NSDate distantFuture];

// Start the run loop.
for(;;) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:endDate];
}


And then the callback will be invoked. Thanks!

-jt

Post Reply