-1

Let's describe my question as below:

  1. I have created a window using CreateWindow, and then also created a Listview and EDIT box to show something; hListview is the handle of the Listview and hEdit is the handle of EDIT box

  2. For now, my program is a single-threaded app;

  3. I have 2 functions void SetListviewText(): to assign different value to Listview's specified item and subitem (the Listview is in report mode);

    and void SetEditBoxText(): to append some text to EDIT box, of course, both of them were updated with SendMessage();

  4. example,

    SetEditBoxText(hEdit, "1111111111111111111111111111111.");
    SetListviewText(hListview, "Description1",19);
    Sleep(1000);
    SetEditBoxText(hEdit, "2222222222222222222222222222222");
    SetListviewText(hListview, "Description2",20);
    SetEditBoxText(hEdit, "33333333333333333333");
    Sleep(5000);
    
  5. question,

    why I only can get the EDIT box content as:

    111111 (Listview text not update now)

    .......1second delay.........

    2222222222222(Listview text not update now)

    3333333333333333(Listview text not update now)

    .......5seconds delay.........

    (Listview text update eventually after 6seconds..)

    Description1

    Description2

Why can't the Listview's text be updated together with EDIT box?

PS: If I only put below code in button click case, it's OK to show it immediately.

SetListviewText(hListview, "Description2",20);

Should I use multiple threads? If yes, how?

Thanks in advance.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
NYwalker
  • 13
  • 1
  • 2
  • 2
    You cannot use `Sleep()` in a GUI thread. Use timers instead. – andlabs Apr 19 '16 at 15:43
  • SendMessage(hListview, LVM_UPDATE, index, 0) or InvalidateRect – nariuji Apr 19 '16 at 16:05
  • Why is it a problem for you that screen updates are not done immediately but Windows is gathering multiple changes and displaying all at once? Are you really calling Sleep() in a GUI thread or is this only for demonstration purpose? You might be interested in reading http://stackoverflow.com/questions/2325894/difference-between-invalidaterect-and-redrawwindow. – Werner Henze Apr 19 '16 at 16:14
  • indeed, the Sleep() is not only for demonstration purpose, because in my program, I need wait for some hardware to reboot. I also want to use timers instead, as the Sleep would cause program freezing. thank you all. I will try to look for the Timer link for using it. – NYwalker Apr 20 '16 at 02:07

2 Answers2

0

Setting the text of the edit control, or the item text in the list control, invalidate those controls (or some area of them). So the next time Windows will get around to paint them, you would see the new content.

If you want to observe the change immediately, simply call UpdateWindow() for both your controls.

Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27
0

When you change the text in the controls, they are "invalidated" which means they need to be repainted with the new values. The painting happens only when the message loop runs. Since you're using Sleep on the one and only thread, the message loop isn't running.

You can set up a timer to change values at specific times, and then return to the message loop, which will allow your updates to happen.

If you must sleep on the main thread, but you want the windows up repaint anyway, you can call UpdateWindow on the controls right before the call to Sleep.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
  • thank you for the explanation. tried the UpdateWindow() and InvalidateRect(hListview,NULL,FALSE), the first is ok, but the second still show the text after some seconds delay. – NYwalker Apr 20 '16 at 01:07
  • @Nywalker: Right. InvalidateRect is probably what the control uses internally to say it needs to repaint. The repaint itself doesn't happen unless the message loop is running (or the somebody calls UpdateWindow). – Adrian McCarthy Apr 20 '16 at 17:52