How to operate list control in a worker thread

G

Guest

I have two threads, one UI thread, the other, the worker thread, which is
responsible for keeping on search things and post the found results to a list
control on the UI.

My leader doesn't allow me to use MFC.

So I have to use SendMessage(LVM_XXX, hListCtrl) in the worker thread, I
guess.

But I failed, there is nothing displaying in the list control in the UI
dialog.

Any people can tell me what's the reason? Thanks very much!
 
?

=?ISO-8859-2?Q?Mihajlo_Cvetanovi=E6?=

zhaoyandong said:
So I have to use SendMessage(LVM_XXX, hListCtrl) in the worker thread, I
guess.

But I failed, there is nothing displaying in the list control in the UI
dialog.

Use PostMessage(hListCtrlParent, UWM_UPDATE_LIST, wParam, lParam) in the
worker thread, and SendMessage(hListCtrl, LVM_...) in the event handler
of user window message UWM_UPDATE_LIST. This might not solve your
problem, but it's the right thing to do.

If wParam and lParam should contain some structs or buffers (like char*)
then create them in worker thread, and delete them in event handler.
 
G

Guest

Thank you very much, Mr Cvetanović .

Your answer is what I really need.

You mean UWM_UPDATE_LIST is a user-defined message, actually listcontrol and
the parent of listcontrol own the same message queue.

I also planned to send the WM_NOTIFY(LVN_INSERTITEM) message to the parent
dialog, but it is also not right solution.

About
---------------
If wParam and lParam should contain some structs or buffers (like char*)
then create them in worker thread, and delete them in event handler.
---------------
This is also what I'm thinking. If I don't create/new the struct or buffer
in the worker thread, but instead, use the stack, and post the address of
the struct as lParam or wParam to the UI. I'm afraid this will cause UI to
lose some results.
Am I right?

Also one of my colleague says, continuously "new" and "delete" will cause
many memory fragments. Is he right? If he is right, How should I avoid this?

Thank you.
 
?

=?ISO-8859-2?Q?Mihajlo_Cvetanovi=E6?=

zhaoyandong said:
You mean UWM_UPDATE_LIST is a user-defined message, actually listcontrol and
the parent of listcontrol own the same message queue.

Yes, that's correct (in this simple case).
---------------
If wParam and lParam should contain some structs or buffers (like char*)
then create them in worker thread, and delete them in event handler.
---------------
This is also what I'm thinking. If I don't create/new the struct or buffer
in the worker thread, but instead, use the stack, and post the address of
the struct as lParam or wParam to the UI. I'm afraid this will cause UI to
lose some results.
Am I right?

Yes, you're right, if you want to PostMessage to pass structures around
then you must use the heap.
Also one of my colleague says, continuously "new" and "delete" will cause
many memory fragments. Is he right? If he is right, How should I avoid this?

If you post structs or classes then they are all of the same size, so
there's no fragmentation. But if you post char* (or char* inside the
struct) AND those buffers are of the various sizes (between 1 byte and 1
megabyte) then there might be fragmentations. However, you shouldn't
think about performances so early, the chances are you wont feel any
slowdown.

So the answer is that he could be right, but that could be irrelevant.

Fragmentation could be avoided if you use buffers of the same size, or
if you "recycle" old buffers, but I repeat you shouldn't worry about
that right now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top