• 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 07:51:15 -0800

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.

  • Follow-Ups:
    • Re: "Thread,eventloop,moviestask"
      • From: Shaun Wexler <email@hidden>
References: 
 >"Thread,eventloop,moviestask" (From: Bartosz Smaga <email@hidden>)

  • Prev by Date: What units are in effect when drawing?
  • Next by Date: Re: "Thread,eventloop,moviestask"
  • Previous by thread: Re: "Thread,eventloop,moviestask"
  • Next by thread: Re: "Thread,eventloop,moviestask"
  • Index(es):
    • Date
    • Thread