• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: "Thread,eventloop,moviestask"
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "Thread,eventloop,moviestask"


  • Subject: Re: "Thread,eventloop,moviestask"
  • From: Shaun Wexler <email@hidden>
  • Date: Sat, 5 Apr 2003 08:11:29 -0800

I thought I should qualify a few points about the timer methods I suggested. The performActionForTimer: method will be called from the timer's thread, not the main thread, so you aren't guaranteed to be able to update the GUI safely, without making other considerations. Also, this all could be rewritten, adding another method which could allow you to add new timers to the existing timer thread, which would then be persistent.
--
Shaun Wexler
MacFOH
http://www.macfoh.com


On Saturday, April 5, 2003, at 07:51 AM, Shaun Wexler wrote:

On Saturday, April 5, 2003, at 01:32 AM, Bartosz Smaga wrote:

What I have to do to make thread that executes (25fps) over the main
eventloop?

In the main thread, you could simply install a timer:

NSTimer *timer = [[NSTimer scheduledTimerWithTimeInterval:animationFrameRate target:self selector:@selector(timedAction:) userInfo:nil repeats:YES] retain];

Although complex, this is a bit more flexible than some simpler methods, and runs threaded, independent of the main run loop:

- (void) runThreadedAnimationForTarget:(id)target withSelector:(SEL)selector framesPerSecond:(double)fps
{
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[target methodSignatureForSelector:selector]];
[invocation setSelector:selector];
[NSThread detachNewThreadSelector:@selector(threadedTimerWithAttributes:) toTarget:self withObject:[NSArray arrayWithObjects:invocation, [NSNumber numberWithDouble:1.0/fps], nil]];
}

- (void) threadedTimerWithAttributes:(NSArray *)attributes
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSInvocation *invocation;
NSTimer *timer;

if (attributes && [attributes count] == 2)
{
invocation = [attributes objectAtIndex:0];
timer = [NSTimer scheduledTimerWithTimeInterval:[[attributes objectAtIndex:1] doubleValue] invocation:invocation repeats:YES];
[invocation setArgument:&timer atIndex:2];
[[NSRunLoop currentRunLoop] run];
}
[pool release];
}

And in your target object, add this method, which will be called (fps) times per second:

- (void) performActionForTimer:(NSTimer *)timer
{
BOOL cancelTimer = NO;

// perform periodic action...

if (cancelTimer)
[timer invalidate]; // causes timer's thread to subsequently exit
}

This allows the target to cancel the timer, at will.

I composed this in Mail to illustrate the concept, but it has not been tested. You may need to add some retain/release messages to the timer and/or invocation; I think they should be okay, though. Hope this helps you out!

Regards,
--
Shaun Wexler
MacFOH
http://www.macfoh.com
_______________________________________________
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.

References: 
 >Re: "Thread,eventloop,moviestask" (From: Shaun Wexler <email@hidden>)

  • Prev by Date: Re: "Thread,eventloop,moviestask"
  • Next by Date: Re: Incomplete APIs
  • Previous by thread: Re: "Thread,eventloop,moviestask"
  • Next by thread: gcc pragma pack() question
  • Index(es):
    • Date
    • Thread