2009-12-11 28 views

回答

4

看一看NSObject類的引用 - 方法開始performSelector

0

我想我也有些代碼的一部分。

這是不幸的是沒有這樣做所需的行爲:

- (void) invokeLater_aux:(NSArray*)functionName_arg 
{ 
    NSLog(@"invokeLater_aux:"); 
    if(functionName_arg != nil && (functionName_arg.count > 0)){ 
     // split the params: 
     NSString* functionNameString = [functionName_arg objectAtIndex:0]; 
     NSLog(@"functionNameString: %@",functionNameString);  
     SEL functionName = NSSelectorFromString(functionNameString);   

     id arg = nil; 
     if(functionName_arg.count > 1){ 
      arg = [functionName_arg objectAtIndex:1]; 
     } 

     // call the function on main thread 
     [self performSelectorOnMainThread:functionName withObject:arg waitUntilDone:YES ];  
    } 
    [functionName_arg release]; 
} 

- (void) invokeLater: (SEL)functionName withObject:(id)arg 
{ 
    NSLog(@"invoke later: %@",NSStringFromSelector(functionName)); 
    [self performSelectorInBackground:@selector(invokeLater_aux:) 
          withObject:[[NSArray alloc] initWithObjects: 
             NSStringFromSelector(functionName),//the function name as String 
             arg,// the function arguments 
             nil] 
    ];  
} 

因爲它開始的invokeLater一個新線程,不等待完成執行主線程,它會在被執行的invokeLater_aux的號召主線程(在上下文切換?)

有可能有n線程使用此代碼,如果你調用ninvokeLater

  • 如何在invokeLater_aux等到主線程完成