// JP opened flex table

Click to See Complete Forum and Search --> : PostMessageThread failure


cilu
March 15th, 2004, 08:46 AM
Hi,

I have a problem with posting messages to a thread created with _beginthreadex. I cannot use CWinThread, so I created a thread like below:

m_hThread = (HANDLE)_beginthreadex(
lpSecurityAttrs, // Security attributes
nStackSize, // stack
SocketThreadProc, // Thread proc
this, // Thread param
dwCreateFlags, // creation mode
&uiThreadId); // Thread ID

I have a method that checks for posted messages

int CSocketThread::CheckPostedMessages()
{
MSG msg;
::ZeroMemory(&msg,sizeof(msg));
// if this works WM_QUIT will have priority over other messages
if (0 != ::PeekMessage(&msg,(HWND)-1, WM_QUIT,WM_QUIT, PM_REMOVE))
{
...
}
else if ( 0 != ::PeekMessage(&msg,(HWND)-1, 0,0, PM_REMOVE))
{
...
}

return 0;
}

The method is called in a while loop in Run().

I post message with

void CSocketThread::PostMessageToThread( ThreadMessages msg, unsigned wp, unsigned lp )
{
BOOL bRet = ::PostThreadMessage((DWORD)m_hThread,msg,wp,lp);
if(!bRet)
{
int error = GetLastError();

if(error == ERROR_INVALID_THREAD_ID)
TRACE("....");
}
}

The wierdest thing of all is that PostThreadMessage return 0, which means an error happened. The call for GetLastError() return also 0, that means the last operation succeded, which is a paradox.

In MSDN it's said that PostThreadMessage fails if the thread doesn't have a queue. The solution for that is "Create an event object, then create the thread. Use the WaitForSingleObject function to wait for the event to be set to the signaled state before calling PostThreadMessage. In the thread to which the message will be posted, call PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE) to force the system to create the message queue. Set the event, to indicate that the thread is ready to receive posted messages." I did that and nothing else happened.

Can anyone help me with this?

Thank you.

Pham Son Thuy
March 15th, 2004, 09:31 AM
I dont know how to resolve this problem. But this is a worker thread and you can try a simple way. Create an event (by CreateEvent) then use that event to notify to your thread. In your thread loop, call WaitForSingleObject to determine when the event fired.

Andreas Masur
March 15th, 2004, 09:55 AM
[Moved thread]

cilu
March 15th, 2004, 10:15 AM
My problem is not threads synchronization. I can not post messages to my thread. Using a CEvent object doen't allow me to send messages. CEvents are for synchronization. What I need is a way to send messages to my thread from outside.

And I don't understand that paradox with PostThreadMessage returning error, but GetLastError indicating ERROR_SUCCES. That's so weird.

Pham Son Thuy
March 15th, 2004, 10:21 AM
PostThreadMessage is used to notify a message to a speficied thread. Event is used to notify an event. So you can use Event to perform the same function as PostThreadMessage in this context -just notify a message to a thread. This is a very common in communication to worker thread.

cilu
March 16th, 2004, 06:37 AM
I managed to solve the problem. It was of course my mistake. I was using the handle to the thread not the ID of the thread as the first parameter of PostThreadMessage.

Thanks anywoy for the tips. I know what you meant with the CEvent, but it wasn't appropriate for my application.

//JP added flex table