Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Thread Message Queue Problem?

0 views
Skip to first unread message

RonaldHall

unread,
Jan 21, 1998, 3:00:00 AM1/21/98
to

Subject: Message Queue Problem
From: ronal...@aol.com (RonaldHall)
Date: Sat, Jan 17, 1998 11:58 EST
Message-id: <19980117165...@ladder02.news.aol.com>

I'm struggling with PostThreadMessage and GetMessage. The main program when
processing the WM_CREATE message starts a thread using _beginthread and saves
the return value as the thread ID of the newly created thread. The main program
then waits on an EventObject. The newly created thread uses PeekMessage to
force creation of a message queue then signals the event object allowing the
main thread to resume. Subsequently due to some event (say WM_PAINT) the main
program attempts to use the PostThreadMessage and the saved thread id (from
_beginthread) to post a message to the threads message queue. PostThreadMessage
is always returning with a 0 (nonzero is supposed to indicate success). When a
place a GetLastError call immediately following the call to PostThreadMessage
it returns a 0 for the error code (which indicates success). The thread
remains suspended at the GetMessage instruction waiting for receipt of a
message. So does anyone know what is going on here? I've included some code
snipits if thats of any help.


WinMain WinProc
...
case WM_CREATE:
...
hev = CreateEvent(NULL, TRUE, FALSE, NULL);
idTextThread = _beginthread(TextDrawing, 0, &hev);
WaitForSingleObject(hev, INFINITE);
CloseHandle(hev);
return 0;

case WM_PAINT:
...
fError = PostThreadMessage(idTextThread, CMD_INITIALIZE, 0, 0);
ErrorCode = GetLastError();

Note fError is a zero after the call and ErrorCode is zero after the call.


Thread

VOID TextDrawing(PVOID phEventObject)
{
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
SetEvent(*((PHANDLE) phEventObject));

while (GetMessage(&msg, NULL, 0, 0))

Thanks in advance
Ron


Richard Krehbiel

unread,
Jan 21, 1998, 3:00:00 AM1/21/98
to

ronal...@aol.com (RonaldHall) writes:

> WinMain WinProc
> ...
> case WM_CREATE:
> ...
> hev = CreateEvent(NULL, TRUE, FALSE, NULL);
> idTextThread = _beginthread(TextDrawing, 0, &hev);
> WaitForSingleObject(hev, INFINITE);
> CloseHandle(hev);
> return 0;
>
> case WM_PAINT:
> ...
> fError = PostThreadMessage(idTextThread, CMD_INITIALIZE, 0, 0);
> ErrorCode = GetLastError();

The problem is that PostThreadMessage wants a thread *id*, not a
thread *handle*. You can get the thread ID by using _beginthreadex
instead of _beginthread.

UINT idTextThreadID;
idTextThread = _beginthreadex(NULL, 0, idTextThread,
&hev, 0, &idTextThreadID);

[ ... ]

fError = PostThreadMessage(idTextThreadID, CMD_INITIALIZE, 0, 0);

The thread must be declared "unsigned __stdcall thread(void *)".

--
Richard Krehbiel, Kastle Systems, Arlington, VA, USA
ri...@kastle.com (work) or ri...@mnsinc.com (personal)

Jason Shannon

unread,
Jan 21, 1998, 3:00:00 AM1/21/98
to

>I'm struggling with PostThreadMessage and GetMessage. The main program
when
>processing the WM_CREATE message starts a thread using _beginthread and
saves
>the return value as the thread ID of the newly created thread. The main
program


_beginthread returns a *handle* to the new thread, *not* the ID. You could
get the new thread to fill a global variable (or something) with its ID
(from GetCurrentThreadId()) before your call to SetEvent so that the
creating thread can use it with PostThreadMessage.

You may be getting a success code from PostThreadMessage because the handle
value you're passing in is actually a valid thread ID as well, but for a
different thread.

--
Jason Shannon

0 new messages