How to detect whether an OS X application is alrea

2019-01-22 08:06发布

Normally an application bundle on OS X can only be started once, however by simply copying the bundle the same application can be launched twice. What's the best strategy to detect and stop this possibility?

On Windows this effect can simply be achieved by the application creating a named resource at launch and then exit if the named resource can't be created, indicating that another process is running that has already created the same resource. These resources are released in a reliable way on Windows when the application quits.

The problem I have seen when researching this is that the APIs on OS X keep state in the file system and thus makes the strategy used on windows unreliable, i.e lingering files after an improper exit can falsely indicate that the application is already running.

The APIs I can use to achieve the same effect on OS X are: posix, carbon and boost.

Ideas?

9条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-22 08:19

A low-level solution is to use flock().

Each instance would attempt to lock a file on startup, and if the lock fails then another instance is already running. Flocks are automagically released when your program exits, so no worries about stale locks.

Note that whatever solution you choose, you need to make a conscious decision about what it means to have "multiple instances". Specifically, if multiple users are running your app at the same time, is that ok?

查看更多
劳资没心,怎么记你
3楼-- · 2019-01-22 08:25

detect if application with same bundleID is running, activate it and close what starts.

- (id)init method of < NSApplicationDelegate >

    NSArray *apps = [NSRunningApplication runningApplicationsWithBundleIdentifier:[[NSBundle mainBundle] bundleIdentifier]];
    if ([apps count] > 1)
    {
        NSRunningApplication *curApp = [NSRunningApplication currentApplication];
        for (NSRunningApplication *app in apps)
        {
            if(app != curApp)
            {
                [app activateWithOptions:NSApplicationActivateAllWindows|NSApplicationActivateIgnoringOtherApps];
                break;
            }
        }
        [NSApp terminate:nil];
        return nil;
    }
查看更多
三岁会撩人
4楼-- · 2019-01-22 08:30

First off, it's “Mac OS X” or “OS X”. There is no such thing as “OS/X”.

Second, Mac OS X doesn't come with Boost; you would need to bundle it with your application.

Third, most of Carbon is not available in 64-bit. This is a clear signal that those portions of Carbon will go away someday (when Apple abandons 32-bit in its hardware). Sooner or later, you will have to either rewrite your app with Cocoa or abandon the Mac.

Normally an application bundle on OS/X can only be started once, however by simply renaming the bundle the same application can be launched twice.

No it can't. Launching the renamed or moved application will simply activate (bring to the front) the process that was already running; it won't start a new, second process alongside the first one.


There are several ways to tell whether an application is already running. In each case, you do this on launch:

  1. Use Cocoa's NSConnection to register a connection with a single constant name. This will fail if the name is already registered. (You can use Foundation from a Carbon app; it's the Application Kit you have to be careful with.)
  2. Use the Process Manager to scan the process list for processes whose bundle identifier match the one you're looking for. The bundle identifier isn't unchangeable, but it's harder to change than the filename or location.
  3. If you're looking to see when someone runs a second copy of yourself, you can use CFNotificationCenter:

    1. Add yourself as an observer for “com.yourdomain.yourappname.LaunchResponse”.
    2. Post a notification under the name “com.yourdomain.yourappname.LaunchCall”.
    3. Add yourself as an observer for “com.yourdomain.yourappname.LaunchCall”.

    In your observation callback for the Call notification, post the Response notification.
    In your observation callback for the Response notification, exit.

    Thus, when the first process starts, it will Call and get no Response; when the second process starts, it will Call, get a Response from the first process, and exit in deference to the first.

查看更多
小情绪 Triste *
5楼-- · 2019-01-22 08:32

This is a combination of Romans' and Jeff's answers for Swift 2.0: If another instance of the app with the same bundle ID is already running, show an alert, activate the other instance and quit the duplicate instance.

func applicationDidFinishLaunching(aNotification: NSNotification) {
    /* Check if another instance of this app is running. */
    let bundleID = NSBundle.mainBundle().bundleIdentifier!
    if NSRunningApplication.runningApplicationsWithBundleIdentifier(bundleID).count > 1 {
        /* Show alert. */
        let alert = NSAlert()
        alert.addButtonWithTitle("OK")
        let appName = NSBundle.mainBundle().objectForInfoDictionaryKey(kCFBundleNameKey as String) as! String
        alert.messageText = "Another copy of \(appName) is already running."
        alert.informativeText = "This copy will now quit."
        alert.alertStyle = NSAlertStyle.CriticalAlertStyle
        alert.runModal()

        /* Activate the other instance and terminate this instance. */
        let apps = NSRunningApplication.runningApplicationsWithBundleIdentifier(bundleID)
        for app in apps {
            if app != NSRunningApplication.currentApplication() {
                app.activateWithOptions([.ActivateAllWindows, .ActivateIgnoringOtherApps])
                break
            }
        }
        NSApp.terminate(nil)
    }

    /* ... */
}
查看更多
叼着烟拽天下
6楼-- · 2019-01-22 08:34

This is extremely easy in Snow Leopard:

- (void)deduplicateRunningInstances {
    if ([[NSRunningApplication runningApplicationsWithBundleIdentifier:[[NSBundle mainBundle] bundleIdentifier]] count] > 1) {
        [[NSAlert alertWithMessageText:[NSString stringWithFormat:@"Another copy of %@ is already running.", [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleNameKey]] 
                         defaultButton:nil alternateButton:nil otherButton:nil informativeTextWithFormat:@"This copy will now quit."] runModal];

        [NSApp terminate:nil];
    }
}

See http://blog.jseibert.com/post/1167439217/deduplicating-running-instances-or-how-to-detect-if for more information.

查看更多
该账号已被封号
7楼-- · 2019-01-22 08:34

There's a mysterious Info.plist key called "Application prohibits multiple instances," but it doesn't seem to work for me. I am writing a CLI application and executing it from within a bundle. Perhaps it would work in a GUI application, but I haven't tried.

查看更多
登录 后发表回答