Some code:
LRESULT CScopeFrame::OnDATACallback(WPARAM wParam, LPARAM lParam)
{
std::function<void()>* callbackPtr = reinterpret_cast<std::function<void()>*>(lParam);
if (callbackPtr && *callbackPtr)
{
(*callbackPtr)();
}
return 0;
}
void sendCallbackMessage(CWnd* pWnd) const
{
if (callBackType == CALLMESSAGE && callbackNoParam)
{
if (pWnd != nullptr)
{
// Post the callback function address via LPARAM
pWnd->PostMessage(WM_USER_ON_DATA_CALLBACK, 0, reinterpret_cast<LPARAM>(&callbackNoParam));
}
}
}
void sendCallbackMessage() const
{
if (callBackType == CALLMESSAGE && callbackNoParam)
{
CWnd* pWnd = AfxGetMainWnd(); // Get the main window pointer
if (pWnd != nullptr)
{
// Post the callback function address via LPARAM
pWnd->PostMessage(WM_USER_ON_DATA_CALLBACK, 0, reinterpret_cast<LPARAM>(&callbackNoParam));
}
}
}
void triggerSendMessage() const
{
if (asyncCallBack)
{
CWnd* pWnd = AfxGetMainWnd(); // Get the main window pointer
//std::sync should use system threead pool minimazing overhead.
std::async(std::launch::async, [this, pWnd]()
{
sendCallbackMessage(pWnd);
});
}
else
{
sendCallbackMessage();
}
};
where
std::function<void()> callbackNoParam;
It works as expected but my question is – is it safe and correct?
I have a limited experience (I did not touch MFC and
Windows for 15years)