无法在Objective-C中生成新线程

时间:2021-07-06 20:59:58

I am launching my JAR with Applescripts that reside inside my objective-c code.

我正在使用位于我的objective-c代码中的Applescripts启动我的JAR。

I want to perform this operation in a new thread (NSThread).

我想在新线程(NSThread)中执行此操作。

note: I have used GCD but it doesn't help me as even the concurrent queue has a dependency on the main thread.

注意:我使用过GCD,但它对我没有帮助,因为即使并发队列也依赖于主线程。

-(void) launchJar{
        NSAppleScript *script = [[NSAppleScript alloc] initWithSource:scriptToLaunch];
    [script executeAndReturnError:nil];

    NSLog(@"hitting this point");
}


int main(int argc, char *argv[]) {
        @autoreleasepool {
            MCMCustomURLSchemeHandler *mcmCustomURLHandler = [[MCMCustomURLSchemeHandler alloc] init];


                  [NSThread detachNewThreadWithBlock:@selector(launchJar) toTarget:[JARLauncher class] withObject:nil];



            return NSApplicationMain(argc, argv);
        }
    }

1 个解决方案

#1


1  

You should put the statements of launchJar into an autorelease pool:

您应该将launchJar的语句放入自动释放池中:

- (void)launchJar {
    @autoreleasepool {
        NSAppleScript *script = [[NSAppleScript alloc] initWithSource:scriptToLaunch];
        [script executeAndReturnError:nil];
        NSLog(@"hitting this point");
    }
}

BTW: You should avoid launching threads with NSThread directly. Try NSOperationQueue or GCD instead.

顺便说一句:你应该避免直接使用NSThread启动线程。请尝试使用NSOperationQueue或GCD。

#1


1  

You should put the statements of launchJar into an autorelease pool:

您应该将launchJar的语句放入自动释放池中:

- (void)launchJar {
    @autoreleasepool {
        NSAppleScript *script = [[NSAppleScript alloc] initWithSource:scriptToLaunch];
        [script executeAndReturnError:nil];
        NSLog(@"hitting this point");
    }
}

BTW: You should avoid launching threads with NSThread directly. Try NSOperationQueue or GCD instead.

顺便说一句:你应该避免直接使用NSThread启动线程。请尝试使用NSOperationQueue或GCD。