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



在必须实时处理用户事件的 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() failn" );

线程信号:

if ( !PostThreadMessage( dwMathThreadID, MATHTHREAD_PROC, NULL, 0 ) ) {
DWORD dwError = GetLastError();
printf( "PostThreadMessage() for math thread: %dn", 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: %dn", GetLastError() );
switch ( msg.message ) {
case MATHTHREAD_PROC:
ProcessMath();
break;
default:
printf( "got math thread exit request %pn", this );
abort();
}
}
printf( "SCGraph::MathThread(): GetMessage = WM_QUITn" );
return msg.wParam;
}

ERROR_MESSAGE_SYNC_ONLY表示消息低于WM_USER。 只需更改消息 ID 即可使帖子正常工作:

#define MATHTHREAD_PROC ( WM_USER + 1 )

相关内容

最新更新