Skip navigation.
 
mlHelp with Timer
FROM : Ted Lowery
DATE : Thu Jan 02 06:05:28 2003

Hi all-

I'm trying to create a process that will run periodically.  I have the
process running in a separate thread, so that it doesn't block the
interface.  It does some network accesses, and can take several seconds
to complete.

I've created a timer which I invalidate after it fires, and then I wish
to re-establish it after the thread completes.  However, re-creating
the timer within the thread (and a separate NSAutoReleasePool) it never
fires.  See code below...

- (void)awakeFromNib
{
    forecast = [[Forecast alloc] init];
    timer = [[NSTimer scheduledTimerWithTimeInterval:10 //seconds
                                              target:self
                                            selector:@selector(update:)
                                            userInfo:nil
                                              repeats:NO] retain];
//    [NSThread detachNewThreadSelector:@selector(updateThread:)
toTarget:self withObject:self];
}

- (void)update:(NSTimer*)aTimer
{
    [timer invalidate];
    [timer release];
    [NSThread detachNewThreadSelector:@selector(updateThread:)
toTarget:self withObject:self];
//    if I re-establish the timer here, it works, but of course, it
fires 10 seconds after the original firing, not 10 seconds after the
thread completes.
    timer = [[NSTimer scheduledTimerWithTimeInterval:10 //seconds
                                              target:self
                                            selector:@selector(update:)
                                            userInfo:nil
                                              repeats:NO] retain];
}

- (void)updateThread:(id)sender
{
    NSAutoreleasePool* subpool = [[NSAutoreleasePool alloc] init];
    [forecast update];
//    if I re-establish the timer here, it is created but never fires.
    timer = [[NSTimer scheduledTimerWithTimeInterval:10 //seconds
                                              target:self
                                            selector:@selector(update:)
                                            userInfo:nil
                                              repeats:NO] retain];
    [subpool release];
}


Any ideas?

Thanks, Ted
_______________________________________________
cocoa-dev mailing list | <email_removed>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

Related mailsAuthorDate
mlHelp with Timer Ted Lowery Jan 2, 06:05
mlRe: Help with Timer David Rio Vierra Jan 2, 06:26
mlRe: Help with Timer Chris Kane Jan 2, 20:34