NSRunningApplication isTerminated bug

  • Hi all,

    I seem to be having a problem with terminating apps and observing the
    'terminated' property of NSRunningApplications. If you run my sample code
    (attached, and below), you will notice that even though Safari has quit
    (its icon has disappeared from the dock) the 'isTerminated' property
    returns NO indefinitely, causing my simple app to hang.

    Am I missing something blindly obvious in my code, or is this a bug with
    the NSRunningApplication?
    I have tried this with different apps, not just Safari and I get the same
    result. In my own app, I'm using the
    runningApplicationsWithProcessIdentifier: method, but it doesn't make a
    difference.

    Thanks for any help

            // make sure Safari is running for this simple test case!
            NSArray *safariApplication [NSRunningApplication runningApplicationsWithBundleIdentifier:@
    "com.apple.Safari"];
            NSLog(@"Running instances of Safari: %@",safariApplication);

            NSRunningApplication *safari = nil;
            // get the 1st item in the array
            if ([safariApplication count] > 0) {
                safari = [safariApplication objectAtIndex:0];
            }

            // Safari's not running or something else, quit
            if (!safari) return 0;

            // while the app is still running, try to quit it
            while (![safari isTerminated]) {
                // log if the app's quit or not (keeps returning NO
    indefinitely)
                NSLog(@"Safari is terminated?
    %@",[safari isTerminated]? @"YES" : @"NO");

                // Try to quit Safari
                [safari terminate];
            }
  • On Jun 7, 2012, at 4:51 AM, Patrick Robertson wrote:

    > I seem to be having a problem with terminating apps and observing the
    > 'terminated' property of NSRunningApplications. If you run my sample code
    > (attached, and below), you will notice that even though Safari has quit
    > (its icon has disappeared from the dock) the 'isTerminated' property
    > returns NO indefinitely, causing my simple app to hang.
    >
    > Am I missing something blindly obvious in my code, or is this a bug with
    > the NSRunningApplication?
    > I have tried this with different apps, not just Safari and I get the same
    > result. In my own app, I'm using the
    > runningApplicationsWithProcessIdentifier: method, but it doesn't make a
    > difference.

    https://developer.apple.com/library/mac/#documentation/AppKit/Reference/NSR
    unningApplication_Class/Reference/Reference.html


    "Properties that vary over time are inherently race-prone. For example, a hidden app may unhide itself at any time. To ameliorate this, properties persist until the next turn of the main run loop in a common mode. For example, if you repeatedly poll an unhidden app for its hidden property without allowing the run loop to run, it will continue to return NO, even if the app hides, until the next turn of the run loop."

    In your sample code, you're polling the property without allowing the main run loop to run. Given that your sample code is a Foundation tool, you don't automatically have a runloop created for you. If this is true of your actual code as well, you'll need to create a runloop and allow it to run (explicitly in your polling loop or rewriting your code to not synchronously block the thread).
previous month june 2012 next month
MTWTFSS
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30  
Go to today