2016-12-01 24 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 你可以看看嗎? –