可以正常发送消息,但在winapi中无法接收

问题描述 投票:0回答:1

这段代码旨在粗略估计从要求 Windows 操作系统将消息排队到窗口队列中到窗口线程实际获得 CPU 时间所需的时间。接收线程有它的 Message-Only 窗口来接收和处理消息。消息通过单独的线程发送。

代码编译完美,消息发送函数返回无错误,但收不到消息。我不需要添加所有初始化和终止线程的代码,因为它们已经过测试可以正常工作。

此外,接收窗口确实可以很好地接收操作系统消息。所以问题只在于没有收到自定义消息。

#define INTER_MESSAGE (WM_USER + 1)
FCriticalSection ThreadsMutexes_A;//UE-style mutex.

struct InterThreadMessagingReportStruct
    {
        bool MessageEverReceived = false;//To check if message received even once.
        
        int Count = 0;
        double Average = 0.0;
        double min = HUGE_VAL;
        double max = -HUGE_VAL;
        void Update(double vlu)
        {
            //Averaging:
            Average = (Average * (double)Count + vlu) / (double)(Count + 1);
            //Updating count.
            Count++;
            //Updating min and max.
            if (vlu < min)min = vlu;
            if (vlu > max)max = vlu;
        }
    };
InterThreadMessagingReportStruct* InterThreadMessagingReportObject = new InterThreadMessagingReportStruct();
    
//This is the ThreadFunction of the sending thread.
DWORD WINAPI SendingThreadFunc(LPVOID lpParam)
{
    while(true)
    {
        DWORD dwTargetThreadID = ISProt0::MainRAM_ThreadsHandlingObject->pData_A->RealThreadID;
        //This was checked already to be correct. No need to show extra code here for that.
        
        static bool once = true;

        double time = _get_time_ms();//A function that returns time based on QueryPerformanceCounter, in ms. Works perfectly.
        if (PostThreadMessage(dwTargetThreadID, INTER_MESSAGE, (WPARAM)time, 0))
        {
            printf("message send properly!");//This is always printed.
        }
        else if (once)
        {
            once = false;
            DWORD error = GetLastError();
            printf(error);//This is never printed.
        }
        Sleep(5);
    }
}

//This is the WndProc of the receiving thread with a Message-Only window.
LRESULT CALLBACK WndProcA(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    //Prep.//Knowing which thread we belong to. 
    switch (message)
    {
    case WM_INPUT:
    {
        //deleted code, but in the original code, this message is received perfectly.
        //I say it works perfectly so that you don't doubt the basic functioning of the thread, window, and messaging.
        break;
    }
    case INTER_MESSAGE:
    {       
        double sending_time = (double)wParam;
        double current_time = _get_time_ms();
        double vlu = current_time - sending_time;
        ThreadsMutexes_A.Lock();
        InterThreadMessagingReportObject->MessageEverReceived = true;//This is never turned true.
        InterThreadMessagingReportObject->Update(vlu);//Value is never updated and min max are fixed at their infinity initialization in the struct.
        ThreadsMutexes_A.Unlock();
        break;
    }
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
}

/*This is the function that prints results after the thread is terminated. (Note that the InterThreadMessagingReportObject
itself is a global variable, and is not deallocated when the thread is eliminated. It is perfectly readable)*/
void PrintResults()
{
    ThreadsMutexes_A.Lock();
    PrintTInteger("Messages received?: ", (InterThreadMessagingReportObject->MessageEverReceived) ? 1 : 0);
PrintTInteger("COUNT: ", InterThreadMessagingReportObject->Count);
PrintTFloat("AVERAGE: ", InterThreadMessagingReportObject->Average);
PrintTFloat("MIN: ", InterThreadMessagingReportObject->min);
PrintTFloat("MAX: ", InterThreadMessagingReportObject->max);
ThreadsMutexes_A.Unlock();
/*
Note that mutex was used here to make sure that the version of the global variable is visible everywhere and not only
local to the editing thread, by asking for an explicit synchronization when accessing data.
*/

/*
Result printed are these:

Messages received?: 0
COUNT: 0
AVERAGE: 0.000000
MIN: inf
MAX: -inf


*/
}

The final relevant information is that this code is run within UE4. The code is made fully with winapi style and not with UE. and is written into WindowsApplication.cpp file in the engine. I wonder if UE4 has some configurations that prevent inter-thread communication outside of the engine api and style of code.
multithreading winapi unreal-engine4 windows-messages
1个回答
1
投票

观察到的行为遵循

PostThreadMessage
文档中规定的合同:

PostThreadMessage 发送的消息

1
与窗口无关。作为一般规则,与窗口无关的消息不能由
DispatchMessage
函数发送。

换句话说:发布到线程的消息不会在恰好由该线程拥有的窗口的窗口过程中观察到。

文档还解释了如何观察线程消息:

发布消息的线程通过调用

GetMessage
PeekMessage
函数来检索消息。


1 应该读作“已发布”,真的。

© www.soinside.com 2019 - 2024. All rights reserved.