Carbon PostEventToQueue sample code prevent from crash of windows and controls

Posted on Updated on

this sample function just presents the example to send command event into queue, this could prevent from thread unsafe problem that cause control or window crashed when you send event from a process to another,

  • remember that control and window events should be processed in main thread.
  • PostHICommand seems like call event handle directly, so use PostEventtoQueue is a better solution

mySendEvent(UInt32 commandID)
{
EventRef nextEvent = NULL;
HICommandExtended nextCmd;

CreateEvent(NULL,kEventClassCommand,kEventCommandProcess,kEventDurationMicrosecond,kEventAttributeUserEvent,&nextEvent);
memset(&nextCmd,0,sizeof(HICommandExtended)) ;
nextCmd.commandID = commandID ;
//nextCmd.menu.menuRef =0 ;
//nextCmd.menu.menuItemIndex = 0 ;
SetEventParameter(nextEvent,kEventParamDirectObject,typeHICommand,sizeof(nextCmd),&nextCmd) ;
PostEventToQueue(GetMainEventQueue(),nextEvent,1) ;
ReleaseEvent(nextEvent) ;
}

廣告

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s