Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Updating ListBox during lengthy operation

6 views
Skip to first unread message

Georges Q

unread,
Jan 13, 2012, 11:04:59 PM1/13/12
to
Through WM_COMMAND i call a function that will process something and put out
results to a listbox.

Im trying to call

// inside the lengthy function
while(PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}

UpdateWindow(hWnd);
RedrawWindow(hWnd, 0, 0,
RDW_INVALIDATE | RDW_ERASE | RDW_ERASENOW | RDW_UPDATENOW);


to update the listbox but apparently its not enough or I dont call
PeekMessage the right way.
I dont know where should i put PeekMessage?
Right now I use GetMessage also in WinMain.


David Lowndes

unread,
Jan 14, 2012, 5:23:00 AM1/14/12
to
>results to a listbox.
>
>Im trying to call
>
>// inside the lengthy function
>...
>
>to update the listbox but apparently its not enough or I dont call
>PeekMessage the right way.

I'd recommend that you change things so that your lengthy process is
run in a worker thread and posts messages to your UI thread to have it
update the list box.

That way you won't have any nasty embedded message loops in places
where they shouldn't be.

Dave

Georges Q

unread,
Jan 14, 2012, 12:56:16 PM1/14/12
to
Thanks Dave,

I suppose you are talking about Multithreading?

With Multithreading it works great, this is the right way to do it?

George


David Lowndes

unread,
Jan 14, 2012, 7:23:16 PM1/14/12
to
>I suppose you are talking about Multithreading?

Yes - though just the 1 worker thread to do your long running
operation.

>With Multithreading it works great, this is the right way to do it?

It's what I'd recommend in the situation you described. It'll
eliminate you needing to call a message pump at inappropriate places
in your code.

Dave

Ulrich Eckhardt

unread,
Jan 16, 2012, 6:51:06 AM1/16/12
to
Am 14.01.2012 18:56, schrieb Georges Q:
> I suppose you are talking about Multithreading?
>
> With Multithreading it works great, this is the right way to do it?

While handling a window message, your UI hangs, because the UI requires
other window messages for operation. For that reason, you should never
block for long during the handling of a single message.

Running longer operations in a thread is one thing, you could also use
e.g. a timer or handle the case that the message loop is idle in order
to advance the lengthy operation. There is no "right way", you need to
pick the way that best suits your requirements.

Uli

Gernot Frisch

unread,
Jan 17, 2012, 4:39:23 AM1/17/12
to
If you want a quick hack, just show a wait cursor.
Thread+Message is the prefered way, I'd say.
0 new messages