在 Windows Nodejs 插件中,我创建了一个窗口来接收消息。
Handle<Value> MakeMessageWindow(const Arguments &args) { // exposed to JS
...
CreateWindow(L"ClassName", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, 0, 0);
...
}
我有一个 wndproc 函数。
Local<Function> wndProc;
LRESULT APIENTRY WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
// pack up the arguments into Local<Value> argv
wndProc->Call(Context::GetCurrent()->Global(), 3, argv);
}
现在我需要发送消息。通常,你会做类似的事情
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
...但这行不通,因为它只会阻塞 v8 事件循环。
如何以一种不会阻止 v8 并允许我在窗口接收消息时调用 JS 函数的方式泵送 Windows 消息?
我认为 libuv 会发挥作用,但我不确定如何从运行在单独线程上的 C 安全地调用 JS 函数,特别是因为uv_async_send不保证每次调用时都会调用回调 http://nikhilm.github.io/uvbook/threads.html#inter-thread-communication,并且我需要确保每次收到窗口消息时都会调用我的 JS 回调。