我想在除了UI线程之外的线程上接收消息。

huangapple go评论74阅读模式
英文:

I want to receive messages on a thread other than the ui thread

问题

我正在尝试创建一个函数,用于检测游戏中的软件信号。
然而,我的线程是与UI线程分离的线程,不应影响UI线程。

我通过创建一个仅包含消息的窗口,并在我的线程上调用GetMessage来进行测试,我可以正常接收消息,直到我点击游戏,但当我点击游戏时,所有消息都无法接收。

我的测试代码如下。

mythread.cpp

HINSTANCE instance = GetModuleHandle(nullptr);

WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.lpfnWndProc = myproc;
wndclass.hInstance = instance;
wndclass.lpszClassName = "test";

DWORD ret = RegisterClassEx(&wndclass);
HWND hwnd = CreateWindowEx(0, "test", "dummy", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, instance, 0);

RAWINPUTDEVICE devices = { 0 };

devices.usUsagePage = 1;
devices.usUsage = 2;
devices.dwFlags = RIDEV_INPUTSINK;
devices.hwndTarget = hwnd;

RegisterRawInputDevices(&devices, 1, sizeof(RAWINPUTDEVICE));

MSG msg;
while (true)
{
    while (GetMessage(&msg, hwnd, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessageW(&msg);
    }
}

myproc

LRESULT CALLBACK myproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    switch (msg)
    {
        case WM_INPUT:
        {
            // 在这里处理输入消息
            // ...
        }
    }
    return DefWindowProc(hwnd, msg, wparam, lparam);
}

请问您能告诉我我做错了什么吗?

【注意】上述代码中已将HTML实体字符转义还原,以便更好地理解代码。

英文:

I'm trying to create a function that detects software signals within a game.
However, my thread is a separate thread from the UI thread and should not affect the UI thread.

I've tested it by creating a message-only window and calling getmessage on my thread, I receive messages fine until I click on a game, but when I click on a game, all messages are not received.

My test code is below.

mythread.cpp

	HINSTANCE instance = GetModuleHandle(nullptr);

	WNDCLASSEX wndclass;
	wndclass.cbSize = sizeof(WNDCLASSEX);
	wndclass.lpfnWndProc = myproc;
	wndclass.hInstance = instance;
	wndclass.lpszClassName = "test";

	DWORD ret = RegisterClassEx(&wndclass);
	HWND hwnd = CreateWindowEx(0, "test", "dummy", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, instance, 0);

	RAWINPUTDEVICE device = { 0 };

	devices.usUsagePage = 1;
	devices.usUsage = 2;
	devices.dwFlags = RIDEV_INPUTSINK;
	devices.hwndTarget = hwnd;

	RegisterRawInputDevices(devices, 1, sizeof(RAWINPUTDEVICE));

	MSG msg;
	while(true)
	{
		while (GetMessage(&msg, hwnd, 0, 0))
		{
			TranslateMessage(&msg);
			DispatchMessageW(&msg);
		}
	}

myproc

LRESULT CALLBACK myproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    switch (msg)
    {
        case WM_INPUT:
        {
            ...
        }
    }
    return DefWindowProc(hwnd, msg, wparam, lparam);
}

Can you please let me know what I did wrong?

答案1

得分: 0

你不能这样做,所有与窗口消息处理有关的工作都必须在创建窗口的线程上进行。你可以使用PostMessage将消息从其他线程(或进程)传递给它,但反过来不行。

我不太确定为什么你想在另一个线程上接收消息,似乎你正在寻找的是生产者-消费者模式,其中你的 GUI 线程将事件产生到某种集合中(例如,使用临界区同步的向量触发事件等待句柄),而你的其他线程会按照它们的到来进行消费。

英文:

You cannot do that, all your window message processing has to be on the thread that created the window. You can pass messages from other threads (or processes) to it using PostMessage, but not the other way around.

I'm not really sure why you want to receive your messages on another thread anyway, it sounds like what you're looking for is the producer-consumer pattern, where your gui thread is producing events into a collection of some kind (a vector synchronized using a critical section that triggers an event wait handle, for example), and your other thread consumes them as they come.

huangapple
  • 本文由 发表于 2023年7月17日 22:05:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705253.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定