Re: Using NSTimer to call the main thread
Re: Using NSTimer to call the main thread
- Subject: Re: Using NSTimer to call the main thread
- From: John Anderson <email@hidden>
- Date: Thu, 27 Mar 2003 18:07:32 -0500
I am not Chris Kane but ...
(1) I believe that is absolutely legal and moreover typical to install
timers and ports into runLoop from other threads. How else would one
run new runLoops if they could not have inputs installed in it from
other threads? I mean if you ran a runLoop that have no inputs it
immediately exits, so it is not like you are going to start the runLoop
first and then add your first input after. Rather we add at least one
input to a runLoop from some mother thread and then launch it in a new
thread. We then are free to add new input signals without worrying
about which thread they come from except to note that Apple's
documentation indicates that NSRunLoop in NOT thread safe. So, if it
is possible that more than one thread could be operating on the runLoop
(say modifying its signal source list) allocating and acquiring an
NSLock for that runLoop may be wise.
(2) If you were using 10.2, the simple way would be to call [NSObject
performSelectorOnMainThread: withObject ...] on the object that you
want to execute in the main thread.
Since, you are using 10.1. I highly recommend using Nathan Day's
NSRunLoopMessenger instead. Frankly, I believe that this is basic
thread functionality that should be build into Cocoa itself. I am now
using it extensively for interthread communication. The code and a
nice example application are available on Nathan's web page:
http://homepage.mac.com/nathan_day/
Truly Yours,
John Anderson
Detroit
On Thursday, Mar 27, 2003, at 13:12 America/Detroit, Peter Robinson
wrote:
I am trying to communicate with the main thread in order to update the
user interface.
From my secondary thread, I can install a one-off NSTimer on the
run-loop of the main thread, like this:
NSTimer *timer = [NSTimer timerWithTimeInterval: 0
target: self selector: @selector(updateValueNow:)
userInfo: nil repeats: NO];
[mainRunLoop addTimer: timer forMode: NSDefaultRunLoopMode];
which seems to work well enough. But I am worried that it isn't legal
to install a timer on a run-loop from another thread. Can I do this?
If not, what is the simplest way to get code called on the main
thread? I am using 10.1.
Thanks,
Peter
_______________________________________________
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.