【问题标题】:How to force kill another application in cocoa Mac OS X 10.5如何在可可 Mac OS X 10.5 中强制杀死另一个应用程序
【发布时间】:2012-10-16 16:35:33
【问题描述】:

我有这个任务,我需要从我的应用程序中杀死另一个我的应用程序,问题是 其他应用程序有一个终止确认对话框(没有要保存的关键数据,仅确认用户打算退出)。

  • 在 10.6+ 上,您将使用:

    bool TerminatedAtLeastOne = false;
    
    // For OS X >= 10.6 NSWorkspace has the nifty runningApplications-method.
    if ([NSRunningApplication respondsToSelector:@selector(runningApplicationsWithBundleIdentifier:)]) {
        for (NSRunningApplication *app in [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.company.applicationName"]) {
            [app forceTerminate];
            TerminatedAtLeastOne = true;
        }
        return TerminatedAtLeastOne;
    }
    
  • 但是在

    // If that didn‘t work either... then try using the apple event method, also works for OS X < 10.6.
    AppleEvent event = {typeNull, nil};
    const char *bundleIDString = "com.company.applicationName";
    
    OSStatus result = AEBuildAppleEvent(kCoreEventClass, kAEQuitApplication, typeApplicationBundleID, bundleIDString, strlen(bundleIDString), kAutoGenerateReturnID, kAnyTransactionID, &event, NULL, "");
    
    if (result == noErr) {
        result = AESendMessage(&event, NULL, kAENoReply|kAEAlwaysInteract, kAEDefaultTimeout);
        AEDisposeDesc(&event);
    }
    return result == noErr;
    

    不能强制退出!!!

那么你可以用什么?

【问题讨论】:

    标签: cocoa kill sigkill nsworkspace appleevents


    【解决方案1】:

    你可以使用我在cocoabuilder上挖掘出来的这个简单代码:

    // If that didn‘t work then try shoot it in the head, also works for OS X < 10.6.
    NSArray *runningApplications = [[NSWorkspace sharedWorkspace] launchedApplications];
    NSString *theName;
    NSNumber *pid;
    for ( NSDictionary *applInfo in runningApplications ) {
        if ( (theName = [applInfo objectForKey:@"NSApplicationName"]) ) {
            if ( (pid = [applInfo objectForKey:@"NSApplicationProcessIdentifier"]) ) {
                //NSLog( @"Process %@ has pid:%@", theName, pid );    //test
                if( [theName isEqualToString:@"applicationName"] ) {
                    kill( [pid intValue], SIGKILL );
                    TerminatedAtLeastOne = true;
                }
            }
        }
    }
    return TerminatedAtLeastOne;
    

    【讨论】:

      猜你喜欢
      • 2011-01-15
      • 2012-03-22
      • 1970-01-01
      • 2010-12-15
      • 2019-05-30
      • 1970-01-01
      • 2010-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多