0

here is my code, i'm new to NSThread, why these code doesn't execute? plz help.

Basically i have a viewcontroller, and i call TESTC() when a button is pressed, you should know TESTC() is a C function. Thanks in advance.

@interface Worker : NSObject
@property (nonatomic, strong) NSThread *thread;
- (void)performTask1 : (void *)context;
@end

@implementation Worker

- (instancetype)init
{
    if (self = [super init]) {
        _thread = [[NSThread alloc] init];
    }
    return self;
}

- (void)performTask1 : (void *)context
{
    [self performSelector:@selector(Task1) onThread:self.thread withObject:(__bridge id _Nullable)(context) waitUntilDone:YES];
}

- (void)Task1
{
    NSLog(@"Task1 proceed");
}
@end


void TESTC()
{
    Worker *kelvin = [[Worker alloc] init];
    [kelvin performTask1:nil];
}
Ray
  • 272
  • 2
  • 13
  • The thread must have a runloop. – KudoCC Jan 06 '16 at 10:53
  • 1
    I'd recommend that you forget immediately anything you learned about NSThread and go straight to Grand Central Dispatch. And forget immediately that you ever saw the method performSelector. Especially with the void* parameter you _will_ run into trouble. – gnasher729 Jan 06 '16 at 11:51
  • @KudoCC how to set a runloop? – Ray Jan 07 '16 at 00:58
  • I finally did this by purrrminator's answer in http://stackoverflow.com/questions/6273020/keep-nsthread-alive-and-run-nsrunloop-on-it – Ray Jan 07 '16 at 01:19

0 Answers0