Avatar of jtrades
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_message (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_message2 (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?
Editors IDEsMicrosoft Development.NET Programming

Avatar of undefined
Last Comment
jtrades

8/22/2022 - Mon
DanRollins

Are the worker-thread windows getting *any* messages?
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.
ASKER CERTIFIED SOLUTION
jtrades

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy