为什么PostThreadMessage()给我一个未列出的错误消息ERROR_MESSAGE_SYNC_ONLY?

时间:2018-08-22 14:16:44

标签: multithreading winapi mfc

在必须实时处理用户事件的MFC应用程序中,创建了一个子线程来进行一些冗长的数学处理。但是,向其发送消息会导致错误1159,winerror.h显示为ERROR_MESSAGE_SYNC_ONLY。为什么?

线程创建:

#define MATHTHREAD_PROC ( 1 )

hMathThread = CreateThread( 0, 0, MathThreadProc, this, 0, &dwMathThreadID );
if( !hMathThread )
    printf( "Math CreateThread() fail\n" );

线程信令:

if ( !PostThreadMessage( dwMathThreadID, MATHTHREAD_PROC, NULL, 0 ) ) {
    DWORD dwError = GetLastError();
    printf( "PostThreadMessage() for math thread: %d\n", dwError );
}

线程代码:

static DWORD WINAPI MathThreadProc( LPVOID pvUserData ) {

    return ( (MyClass*) pvUserData )->MathThread();
}

DWORD MyClass::MathThread() {

    MSG msg;
    int iRV;

    // https://msdn.microsoft.com/en-us/library/ms644946(v=vs.85).aspx
    iRV = PeekMessage( &msg, NULL, WM_USER, WM_USER, PM_NOREMOVE );

    while ( iRV = GetMessage( &msg, 0, 0, 0 ) ) {

        if ( iRV == -1 )
            printf( "GetMessage() = -1: %d\n", GetLastError() );

        switch ( msg.message ) {

        case MATHTHREAD_PROC:
            ProcessMath();
            break;

        default:
            printf( "got math thread exit request %p\n", this );
            abort();
        }
    }

    printf( "SCGraph::MathThread(): GetMessage = WM_QUIT\n" );

    return msg.wParam;
}

1 个答案:

答案 0 :(得分:0)

ERROR_MESSAGE_SYNC_ONLY表示该消息在WM_USER以下。只需更改消息ID即可使帖子生效:

#define MATHTHREAD_PROC ( WM_USER + 1 )