Add item to MFC list box from thread?

Pages: 12
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 use SendThreadMessage.
Have a look at simpler implementation
Control variable for list box is m_list
I want to m_list.AddString(L"A");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

void CCMFCApplication1Dlg::OnBnClickedButton1()
{
	AfxBeginThread(MyThreadProc, this);
 
	// OnOK();
}
 
UINT MyThreadProc(void *pParam)
{
	CCMFCApplication1Dlg* pThis= (CMFCApplication1Dlg*)pParam;
 
	// Use pThis
       //how do I m_list.AddString(L"A") from this thread?
	return 0;
}
Last edited on
Use pThis to send a message.
pThis->SendMessage with the LB_ADDSTRING msg
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.
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void CCMFCApplication1Dlg::OnBnClickedButton1()
{
	AfxBeginThread(MyThreadProc, this);
 
	// OnOK();
}
 
UINT MyThreadProc(void *pParam)
{
	CCMFCApplication1Dlg* pThis= (CMFCApplication1Dlg*)pParam;
 
	pThis->SendMessage(LB_ADDSTRING, (WPARAM)&h);
	return 0;
}
Last edited on
Bumping, hope some one can help.
now where do I get this message sent by SendMessage()?
I don't get this? You send the message when you have something to send?

where h is a CString variable
That's wrong. See:

https://msdn.microsoft.com/en-us/library/windows/desktop/bb775181(v=vs.85).aspx

It needs to be a null terminated string, not a CString.
I mean when I SendMessage() does the list box gets updated itself by just this function or I have to do something else afterwards aswell?

I will correct and use null terminated string, thanks for correcting.
Yes, if the message is correct you should see the added string.

You may check for the return value:

https://docs.microsoft.com/en-us/windows/desktop/Controls/lb-addstring

[EDIT]
You need to send the LPARAM not WPARAM
Last edited on
1
2
CMFCApplication1Dlg* pThis = (CMFCApplication1Dlg*)Param;
pThis->SendMessage(LB_ADDSTRING, (LPARAM)host);

where host is char host[1025]; and holds IP address/hostname
I still do not see anything shown on the list box.
What could be wrong here?

I get "The data area passed to a system call is too small" from GetLastError()
Last edited on
What does this particular pThis->SendMessage(...) do? Normally it is this:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms644950(v=vs.85).aspx
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.
The SendMessage(...) you are using must be defined within CCMFCApplication1Dlg or base class. So, where/how is it defined?

This (LPARAM)host probably doesn't do what you think it does.
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
Try this:

pThis->SendMessage(LB_ADDSTRING, 0, (LPARAM)host);

The third parameter seems to be the LPARAM.
I gave this a try already, but no effect.
Is it possible that the problem lies with this pointer
1
2
3
4
5
6
void CCMFCApplication1Dlg::OnBnClickedButton1()
{
	AfxBeginThread(MyThreadProc, this);//this pointer passed here without initialization 
 
	// OnOK();
}


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.
The message goes m_list.m_hWnd, not this.
Pages: 12