活动监视器没有响应

发布于 2024-08-13 07:05:55 字数 1614 浏览 5 评论 0原文

当我以 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);

when i'm calling the below function in a time interval of 1 second, it would cause Not Responding in Activity Monitor, anyone can tell me why and suggest a solution?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦爱 2024-08-20 07:05:55

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

编辑

不太确定是什么原因造成的,但为了解决此问题,您可以比较 PID 而不是序列号。使用 [[NSProcessInfo processInfo] processIdentifier] 轻松获取自己的 PID

profile it with instruments (sampler instrument), see where it spends its time.

Edit:

Not exactly sure what's causing it, but for a workaround, you could compare PIDs instead of serials. It's easy enough to get your own PID with [[NSProcessInfo processInfo] processIdentifier]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文