Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Error Using PostMessage from within a Class Defined in a Worker Thread

Avatar of jtrades
jtrades asked on
.NET ProgrammingMicrosoft DevelopmentEditors IDEs
2 Comments1 Solution528 ViewsLast Modified:
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?
Avatar of jtrades

Our community of experts have been thoroughly vetted for their expertise and industry experience.

This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answers