Kami
Kami

Reputation: 6059

Instance variable to stop a running thread

Header :

@interface CodeTest : NSObject {
BOOL cancelThread;
}

-(void) testCode;
-(void) stopRunning;
-(void) startRunning;

@property (assign) BOOL cancelThread;
@end

Implementation :

-(void)stopRunning{
    self.cancelThread = YES;
}

-(void)startRunning{
    self.cancelThread = NO;

    [NSThread detachNewThreadSelector:@selector(testCode) toTarget:self withObject:nil];

}
-(void)testCode{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSLog(@"STARTED");
    /* Std C99 code */
    while( conditions ){
            if(self.cancelThread){
                 conditions = NO;
            }
            ...
            ...
    }
    /* end of C code */
    [pool release];
}

As you can see, it tires to execute testCode on a separate thread and using the cancelThread BOOL as a stop execution flag in the separate thread.

I have the following issue :

There is something missing ?

UPDATE : It's not related to missing {}'s as one of you suggested... The code works perfectly without the if(self.cancelThread){ conditions = NO; }.

Upvotes: 0

Views: 209

Answers (2)

bbum
bbum

Reputation: 162712

What do you mean by " /* Std C99 code */"?

If that code is really being compiled as C99 code, then self.cancelThread is problematic because it is an Objective-C expression. First, it is the equivalent of [self cancelThread], a method call and, secondly, it requiresself` which wouldn't be present in the body of a C99 function.

However, given that the code you showed has it in a method, the comment doesn't make sense.

Upvotes: 1

David Gelhar
David Gelhar

Reputation: 27900

-[CodeTest setCancelThread:]: unrecognized selector.

Means that you don't have a setter defined for the cancelThread property. You're missing

@synthesize cancelThread;

(in your @implementation section)

Upvotes: 3

Related Questions