Re: Best way to push action from 1 thread to another?
Re: Best way to push action from 1 thread to another?
- Subject: Re: Best way to push action from 1 thread to another?
- From: email@hidden
- Date: Thu, 09 May 2002 08:54:35 -0700
"Cryx" <email@hidden> wrote:
|Try using true notifications sent via a notification center. The
|receiver is hooked nicely into your event loop, ala what you are doing
|now.
Mike Laster replied:
|Did NSNotificationCenter change? The last time I looked at it, it wasn't
|hooked into the run loop. Whenever a notification was posted, the callout
|to registered listeners was executed in the context of the thread that
|posted the message, which is not necessarily the thread of the listener.
This matches my experience: NSNotificationCenter processes the notification *immediately*, delivering the notifications as simple method calls directly descended from the call that posts the notification, with the calls executed in the same thread that posted the notification.
The official way to do cross-thread execution is distributed objects. For what the original poster wants, that seems to be overkill. Since what he wants is simply to get the notification into the UI thread, there is a simpler way: post an NSApplicationDefined event to the event queue instead of a notification to the notification center. It's documented that *any* thread may post an event safely, and, being an event, it will be handled specifically by the UI thread. (I don't recall where I found it stated explicitly that posting an event is thread-safe. However, [NSApplication postEvent:atStart: says "This method can also be called in subthreads.") Receiving the event requires overriding NSApplication's "sendEvent:" method, since such events aren't dispatched anywhere. But the "notification" event will successfully--and safely--cross to the UI thread.
Glen Fisher
_______________________________________________
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.