1

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

1 に答える 1

0

楽器(サンプラー楽器)でプロファイリングし、どこで時間を費やしているかを確認します。

編集

何が原因なのか正確にはわかりませんが、回避策として、シリアルの代わりに PID を比較できます。独自の PID を取得するのは簡単です[[NSProcessInfo processInfo] processIdentifier]

于 2009-11-30T05:45:29.837 に答える