2009-11-30 8 views
1

Lorsque j'appelle la fonction ci-dessous dans un intervalle de temps de 1 seconde, cela provoquerait Ne pas répondre dans Activity Monitor, tout le monde peut me dire pourquoi et proposer une solution?Le moniteur d'activité ne répond pas

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

Répondre

0

profil avec des instruments (instrument échantillonneur), voir où il passe son temps.

Modifier:

pas exactement ce qui cause, mais pour une solution de contournement, vous pouvez comparer des publications en série au lieu PIDs. Il est assez facile d'obtenir votre propre PID avec [[NSProcessInfo processInfo] processIdentifier]

+0

Je parviens à retrouver son GetProcessForPID (pid, & myPSN) à l'origine ... mais comment puis-je faire? – Daniel