2013-07-08 44 views
0

在我的應用程序中,我繼承了NSWindow類。這是爲了覆蓋默認的按鍵事件。NSWindow keyDown:崩潰

//mywindow.h 
@interface TWindow: NSWindow 
{ 
} 

- (void)keyDown: (NSEvent *)pEvent; 
@end 


//mywindow.mm 
- (void)keyDown:(NSEvent *)theEvent 
{ 
    //Detect Control-W and Control-Q and handle them. 
    unsigned short keycode;  ///< Key code. 
    NSString *  charigmtch;  ///< Character ignoring matches. 
    NSString *  character; 
    BOOL   cmdkeydown;  ///< check if the command key is down. 
    BOOL   shiftkeydown; ///< Check if shift key is down. 

    //Get the keycode of Control-W 
    keycode  = [theEvent keyCode]; 

    //get character ignoring match. 
    charigmtch = [theEvent charactersIgnoringModifiers]; 

    //get character 
    character = [theEvent characters]; 

    cmdkeydown  = ([[NSApp currentEvent] modifierFlags] & NSCommandKeyMask) == NSCommandKeyMask; 
    shiftkeydown = ([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask) == NSShiftKeyMask; 
    //Get the keycode of Control 
    if(cmdkeydown && 12 == keycode && ![character compare:@"q"] && ![charigmtch compare:@"q"]) { 

     //CloseWithConfirm shows message box confirming quit. 
     [self CloseWithConfirm]; 

    } else if (keycode == 48) { 
     //Tab key is pressed. 

      //This AppDelegate is application delegate and also NSWindowDelegate. 
      AppDelegate * delegate;  ///< Delegate. 

     //Get the delegate from the window. 
     delegate = (AppDelegate *)[self delegate]; 

     //Shift key is not down. 
     if(!shiftkeydown) { 
      //Tab key is pressed. 
      [delegate TabKeyPressed]; 
     } else { 
      //Shift-Tab key is pressed. 
      [delegate ShiftTabKeyPressed]; 
     } 
    } 


    //Handle the other key. 
    [super keyDown:theEvent]; //Line E 
} 

當我運行下面的示例測試案例:

void TestCase() 
{ 
    MyThread thread1, thread2, thread3, thread4; 

    //Run thread 1 
    thread1.Execute(); 

    //Run thread 2 
    thread2.Execute(); 

    //Run thread 3 
    thread3.Execute(); 

    //Run thread 4 
    thread4.Execute(); 

} 

//Following function is executed in thread function. 
void Perform() 
{ 
    //This adds the data in table view. 
    AddRowInTableView ("Test"); 
} 

此代碼運行罰款大部分的時間。但有時候,它會在代碼中標出的E行崩潰。 我無法找到這次崩潰的原因。誰能幫我?

我正在使用Mac OS X Mavericks預覽版和Xcode 5進行調試和構建。

+0

不要更新輔助線程中的用戶界面 –

+0

是否所有操作都與輔助線程上的用戶界面限制有關或者只有某些操作不應在輔助線程中執行? – doptimusprime

+0

在Cocoa應用程序中,主線程運行用戶界面,即在主線程上處理所有繪圖和所有事件。 –

回答

3

不要更新輔助線程中的UI。所有繪圖和UI事件都必須在主線程上處理。

+0

感謝您的回答。你能提供一些鏈接嗎?我正在將應用程序從Windows移植到Mac OS X.在Windows上,不存在這樣的問題。 – doptimusprime

+0

在[Cocoa Performance Guidelines](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaPerformance/Articles/UnblockUI.html)和http://(https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaPerformance/Articles/UnblockUI.html)中查看Unblocking Your User Interface stackoverflow.com/questions/9761953/running-a-cocoa-gui-in-a-non-main-thread –

+0

在AddRowInTableView中,現在UI正在主線程中更新。這個問題仍然存在。這個問題並不一致。它在某個時間點擊,但在主線程中。 – doptimusprime