My thread listens and accepts new client connections on sockets, I would like every client added to a list box. Problem is I cannot add items from threads (as I have read on various forums). I need to send/post message?
Please guide me. I am using VS2013 C++ MFC on windows.
Thanks
You should update the UI from the main thread. Other threads have to send a message to the main thread and not update windows directly.
You can't call m_list.AddString, because that does SendMessage with m_list's window handle. You should use SendThreadMessage as I mentioned before. I can't write the code for you because I don't use Windows anymore.
Thanks for responding,
As Thomas1965 stated, I am sending message with LB_ADDSTRING pThis->SendMessage(LB_ADDSTRING, (WPARAM)&h);
where h is a CString variable, now where do I get this message sent by SendMessage()?
So I can finally update from main thread.
Its pretty simple server program, the thread listens on socket and accepts connection. The IP address of client should be placed on list box.
From the thread I cannot directly access list box control variable so I have to use SendMessage().
Now Ideally all I should have to do is call SendMessage whenever a new client is connected and my list box will be updated.
But problem is sendmessage does nothing at all.
I think I need to understand the whole method in a better way before trying to implement it, can you please guide me towards some helpful resource?
Thomas pointed to a good tutorial but seems like I need something more specific or brief to fully understand the concept.
Thanks
Actually the this is invisibly passed to the called member function (as the first parameter). So it needs not initialization. Though, the CCMFCApplication1Dlg object must exist while it is accessed from the thread. I don't know what pThis->SendMessage(...) is doing so it is hard to tell whether it is safe to call it from the thread.