我正在嘗試啓動支持應用程序的列表。然後,我必須等到這些應用程序完成啓動後再繼續執行代碼。以下是我現在所擁有的:啓動外部應用程序並等待它們啓動
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
int i = 0;
while (i < [launchApps count]) {
[ws launcApplication: [launchApps objectAtIndex: i]];
++i
}
while (![self appsFinishedLaunching]) {
NSLog(@"loop");
sleep(1);
}
再後來:
- (BOOL)appsFinishedLaunching
{
BOOL doneLaunching = NO;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int i = 0;
while (i < [launchApps count]) {
NSWorkspace *nws = [NSWorkspace sharedWorkspace];
NSArray *runningApps = [NSArray arrayWithArray:[nws runningApplications]];
for (NSRunningApplication *app in runningApps) {
if ([[app localizedName] isEqualToString:[launchApps objectAtIndex:i]]) {
if ([app isFinishedLaunching]) {
[launchApps removeObjectAtIndex:i];
break;
}
}
}
++i;
}
if ([launchApps count] == 0)
doneLaunching = YES;
[pool drain];
return doneLaunching;
}
如果我跑手動打開launchApps所有的應用程序我控制檯中看到一個「循環」,一切都很好。如果我運行此代碼並讓它啓動應用程序,它會進入無限循環。看起來我的共享工作區中的NSRunningApplications對象沒有被更新,我不知道爲什麼。我錯過了什麼?
你正在爲他們「等待」或「吃東西」嗎?標題有點有趣。 – Blender
錯別字,對不起!感謝克里斯的編輯:) – Kris