活动监视器没有响应

时间:2009-11-30 04:08:11

标签: objective-c macos activity-monitor

当我以1秒的时间间隔调用以下函数时,它会导致活动监视器中没有响应,任何人都可以告诉我原因并建议解决方案?

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 :(得分:0)

用仪器(采样器仪器)对其进行分析,看看它花费的时间。

修改

不完全确定导致它的原因,但是对于解决方法,您可以比较PID而不是连续出版物。使用[[NSProcessInfo processInfo] processIdentifier]

获取自己的PID很容易