2016-12-01 52 views
0

我正在启动带有驻留在我的objective-c代码中的Applescript的JAR。无法在目标中产生新线程 - C

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

请注意:我已经使用过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

,你应该把launchJar的语句转换成一个自动释放池:

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

BTW:你应该避免与NSThread直接启动线程。尝试使用NSOperationQueue或GCD。

+0

我遵循你的建议,并得到它运行。我在发布launchJar方法的位置也遇到了问题。 解决这个问题让我意识到另一个问题:http://stackoverflow.com/questions/40904677/how-do-i-ensure-that-i-close-my-app-only-when-all-the-threads -have-finished-exec 你可以看看吗? –