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.
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.