【发布时间】:2021-09-18 18:34:40
【问题描述】:
如何在收到WM_COPYDATA消息“即时”时返回响应并调用函数?
我尝试使用chrono,但发送消息的应用程序仅在sendCommand函数执行后才收到响应。
#include <thread>
#include <future>
#include <iostream>
void sendCommand(std::chrono::seconds delay, std::string cmd)
{
std::this_thread::sleep_for( delay );
std::cout << "\nThe waited command is =" << cmd;
}
switch (msg)
{
case WM_COPYDATA:
{
OutputDebugStringW(L"\nWM_COPYDATA!");
PCOPYDATASTRUCT pcds = reinterpret_cast<PCOPYDATASTRUCT>(lParam);
//....
auto s1 = std::async(std::launch::async, sendCommand, std::chrono::seconds(5), "Command1");
return 1;
}
}
【问题讨论】:
-
在您的代码示例中,您没有对
pcds做任何事情(它指向WM_COPYDATA提供的数据)。函数sendCommand是否可以访问这些数据?您的问题的答案在很大程度上取决于此。在我的回答中,我假设您不想丢弃WM_COPYDATA传递的数据,但您希望在函数sendCommand被调用时访问该数据。这使得必须复制该数据,因为原始指针仅在WM_COPYDATA处理程序内有效。