【问题标题】:Activity monitor not responding活动监视器没有响应
【发布时间】:2010-12-21 12:39:33
【问题描述】:

当我以 1 秒的时间间隔调用以下函数时,会导致 Activity Monitor 中的 Not Responding,谁能告诉我原因并提出解决方案?

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);

【问题讨论】:

    标签: objective-c macos activity-monitor


    【解决方案1】:

    使用乐器(采样器乐器)对其进行分析,看看它在哪里花费时间。

    编辑

    不完全确定是什么原因造成的,但作为一种解决方法,您可以比较 PID 而不是序列号。使用[[NSProcessInfo processInfo] processIdentifier]获得自己的 PID 很容易

    【讨论】:

    • 我设法找到它的 GetProcessForPID(pid, &myPSN) 导致它...但我该怎么做呢?
    猜你喜欢
    • 2012-03-30
    • 2014-01-10
    • 2011-04-24
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    相关资源
    最近更新 更多