jtrades
asked on
Error Using PostMessage from within a Class Defined in a Worker Thread
I have a Windows program (developed using MSVS 2008 in C) composed of three threads:
1) Main (GUI) windows thread
2) Windows worker thread that acts as a TCP server
3) Windows worker thread that acts as a TCP client
Each worker thread has its own window procedure to handle messages. In addition, the TCP client & server code are defined in separate classes. Each class is used in its corresponding worker thread. Finally, I save a copy of the thread window handles in globals, which I use with PostMessage when I need them.
The TCP client thread gathers data from a remote source and places it in global cache. It then posts a message to the server thread to read this data, so that it can distribute it to those connected to this server. The TCP code works, but the PostMessage does not.
I did some further testing and this is what I found.
* Calling PostMessage directly from within any class function does not work (even though PostMessage does not return an error)
void CServerClass::cache_messag e (char message[])
{
PostMessage (hwnd, WM_SEND, 0, 0);
}
* Calling PostMessage directly inside the window procedure works but outside a class function
LRESULT CALLBACK ClientWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_XXX) {
PostMessage (hwnd, WM_SEND, 0, 0);
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
* Calling PostMessage inside a non class function works
void pmessage (void)
{
PostMessage (hwnd, WM_SEND, 0, 0);
}
* Calling PostMessage indirectly from within a class function works. That is, if I define a nonclass function that calls PostMessage, and then call this function from within a class function, this nonclass function's PostMessage succeeds.
void CServerClass::cache_messag e2 (char message[])
{
//PostMessage (hwnd, WM_SEND, 0, 0); //xxx does not work
pmessage (); // this works
}
Thus, calling PostMessage only works if calling from within a nonclass function. Does any know why this is? Is it possible to use PostMessage directly from within a class? What other methods may I use?
1) Main (GUI) windows thread
2) Windows worker thread that acts as a TCP server
3) Windows worker thread that acts as a TCP client
Each worker thread has its own window procedure to handle messages. In addition, the TCP client & server code are defined in separate classes. Each class is used in its corresponding worker thread. Finally, I save a copy of the thread window handles in globals, which I use with PostMessage when I need them.
The TCP client thread gathers data from a remote source and places it in global cache. It then posts a message to the server thread to read this data, so that it can distribute it to those connected to this server. The TCP code works, but the PostMessage does not.
I did some further testing and this is what I found.
* Calling PostMessage directly from within any class function does not work (even though PostMessage does not return an error)
void CServerClass::cache_messag
{
PostMessage (hwnd, WM_SEND, 0, 0);
}
* Calling PostMessage directly inside the window procedure works but outside a class function
LRESULT CALLBACK ClientWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_XXX) {
PostMessage (hwnd, WM_SEND, 0, 0);
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
* Calling PostMessage inside a non class function works
void pmessage (void)
{
PostMessage (hwnd, WM_SEND, 0, 0);
}
* Calling PostMessage indirectly from within a class function works. That is, if I define a nonclass function that calls PostMessage, and then call this function from within a class function, this nonclass function's PostMessage succeeds.
void CServerClass::cache_messag
{
//PostMessage (hwnd, WM_SEND, 0, 0); //xxx does not work
pmessage (); // this works
}
Thus, calling PostMessage only works if calling from within a nonclass function. Does any know why this is? Is it possible to use PostMessage directly from within a class? What other methods may I use?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It is unusual for a worker thread to process GUI functions... unusual problems always arise and this might be one of them.
For diagnostic, try a SendMessage -- with a breakpoint in the target's Winproc. Also, use Spy++ to watch the messages.
As to class vs. non-class... it is possible that a base-class implementation is overiding the normal Win32 API fn. You can use:
::PostMessage( hwnd, WM_SEND, 0,0); // two leading colons
to be sure you use the Win API fn rather than some overridden method. Also, attempting to single-step into the call might reveal what code is actually handling it.