[Openmcl-devel] Bridge and NSTimer

Gary Byers gb at clozure.com
Tue May 1 16:56:31 PDT 2007

If you're just allocating the NSTimer in the lisp listener thread, the
NSTimer will fire in the listener thread's runloop.  The listener
thread is probably not running its runloop, and therefore the timer
is playing to an empty house.

You -could- tell the listener to run its runloop, or you could
create the timer in the event thread.  (The event thread is always
"running its runloop", which is how it gets event messages, hears
other timers fire, etc.)

A couple of years ago, someone was trying to do Cocoa-based animation
and drawing from multiple threads, and we took the "tiny.lisp" example
and persuaded it to create lots of simple windows and draw simple
polygons in them, using NSTimers to animate the drawing.  I think
that I still have that code somewhere; the animation itself isn't
very interesting and there were race conditions all over (e.g.,
what, if anything, happens if a timer fires while its window is
being closed ?) but it does show how threads/runloops/timers interact
and one approach to getting them to do so.

On Tue, 1 May 2007, Phil wrote:

> The wrong one? Seriously, I see your point... is creating the timer on the 
> main thread (i.e. create a new method to create the timer and then call it 
> via "performSelectorOnMainThread:withObject:waitUntilDone:") the right way to 
> do this or is there a better/alternate way?
> Thanks,
> Phil
> On May 1, 2007, at 5:37 PM, Gary Byers wrote:
>> What thread does that code run in ?  (I guess that the more relevant
>> question is "what NSRunloop is the NSTimer associated with ?")
>> On Tue, 1 May 2007, Phil wrote:
>>> Is there anything special to getting NSTimer to work via the bridge?  I 
>>> don't see anything wrong with this code but the timer doesn't seem to be 
>>> firing.
>>> (defclass timer-test (ns:ns-object)
>>> ()
>>> (:metaclass ns:+ns-object))
>>> (ccl::define-objc-method ((:void :timer-fired the-timer) timer-test)
>>> (#_NSLog #@"timer fired"))
>>> (setf mytimer-objc (objc:make-objc-instance "TimerTest"))
>>> (objc:send (objc:@class "NSTimer")
>>> 	   "scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:"
>>> 	   1.0d0
>>> 	   mytimer-objc
>>> 	   (objc:@selector "timerFired:")
>>> 	   nil
>>> 	   nil)
>>> Thanks,
>>> Phil

More information about the Openmcl-devel mailing list