Монитор активности не отвечает - PullRequest
1 голос
/ 30 ноября 2009

, когда я вызываю указанную ниже функцию с интервалом в 1 секунду, это приведет к тому, что в Activity Monitor не будет ответа, кто-нибудь может сказать мне, почему и предложить решение?

NSWorkspace* workspace = [NSWorkspace sharedWorkspace];

NSDictionary* currentAppInfo      = [workspace activeApplication];

//get the PSN of the current app
UInt32 lowLong                    = [[currentAppInfo objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
UInt32 highLong                   = [[currentAppInfo objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
ProcessSerialNumber currentAppPSN = {highLong,lowLong};


//grab window information from the window server
CFArrayRef windowList             = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
ProcessSerialNumber myPSN         = {kNoProcess, kNoProcess};
//loop through the windows, the window list is ordered from front to back
for (NSMutableDictionary* entry in (NSArray*) windowList)
{
    int pid = [[entry objectForKey:(id)kCGWindowOwnerPID] intValue];
    GetProcessForPID(pid, &myPSN);

    //if the process of the current window in the list matches our process, get the front window number
    if(myPSN.lowLongOfPSN == currentAppPSN.lowLongOfPSN && myPSN.highLongOfPSN == currentAppPSN.highLongOfPSN)
    {
        NSNumber* windowNumber    = [entry objectForKey:(id)kCGWindowNumber];
        NSString* applicationName = [entry objectForKey:(id)kCGWindowOwnerName];
        NSLog(@"The current app is %@ and the window number of its front window is %@.",applicationName,windowNumber);
        //break because we only want the front window
        break;
    }
}
CFRelease(windowList);

1 Ответ

0 голосов
/ 30 ноября 2009

профилируйте его с помощью инструментов (инструмент сэмплера), посмотрите, где он проводит время.

Редактировать

Не совсем уверен, что является причиной этого, но для обходного пути вы могли бы сравнить PID вместо серийных номеров. Достаточно просто получить свой PID с [[NSProcessInfo processInfo] processIdentifier]

...