C++子线程让主线程执行回调

1, 生产者,消费者模型,数据队列

2,窗口消息

void UiCtx::SendFunc(std::function func)
{
    EventFunc *evt = new EventFunc();
    evt->func = func;
    ::SendMessage(wxTheApp->GetTopWindow()->GetHWND(), WM_MSG_KING_FUNC, 0, (LPARAM)evt);
}

void UiCtx::PostFunc(std::function func)
{
    EventFunc *evt = new EventFunc();
    evt->func = func;
    ::PostMessage(wxTheApp->GetTopWindow()->GetHWND(), WM_MSG_KING_FUNC, 0, (LPARAM)evt);
}

class EventFunc
{
public:
    std::function func;
};

bool MainFrame::HandleMswKing(wxWindow *win, WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
{
    if (nMsg == WM_MSG_KING_FUNC)
    {
        EventFunc *evt = (EventFunc *)lParam;
        evt->func();
        delete evt;
        return true;
    }
    return false;
}
 

你可能感兴趣的:(C++子线程让主线程执行回调)