locked
how to update the display of listview RRS feed

  • Question

  • I have a listview containing two columns.  1st column contains process names, the second column contains their status.  My program goes through the processes in the first column, checks them, and then updates the column next to each process as it goes on.  Each process checks about 10 seconds.  I'd like to change the color of each cell in the second column (the status of each process).  The program correctly changes the colors, but the color changes are not shown until the very last row is processed.  I'd like each cell in the second column of each row to be updated as soon as I issue the command, not at the very end.  I don't see any "Refresh" or "Update" methods for listviews.  How does one do this?

     

    Thanks.

    Friday, May 20, 2011 9:05 AM

Answers

  • Hi TasaHammer,

    That same thread is in fact the UIThread. So the UI won't update until all work completes.

    If you only want to display the update on the UI, you can simply add the following code to the exact point when you want the UI to update.

    this.Dispatcher.Invoke(new Action(() => { }), System.Windows.Threading.DispatcherPriority.Background);
    

    The UI will still freeze but at least the changes will be reflected on the UI.

     

    Hope this helps.

    Best regards,

     


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by TasaHammer Friday, May 27, 2011 3:54 AM
    Friday, May 27, 2011 2:51 AM

All replies

  • Are you using ObservableCollection ?ObservableCollection sends automatic notification when a new item is inserted. Thus notifies the Control to update the list.
    Friday, May 20, 2011 9:13 AM
  • I actually am. But the updates of the right column are happening past that stage. In the first stage listview gets populated and remains the same size. In the second stage the values in the right column get changed one by one. I'd like the update to be visualized right away instead of one batch update at the very end.
    Friday, May 20, 2011 5:07 PM
  • have you implemented INPC?
    Friday, May 20, 2011 11:05 PM
  • public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(String info)
        {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null)
          {
            handler(this, new PropertyChangedEventArgs(info));
          }
        }

    in your class inherited INotifyPropertyChanged and add above function in it, then in each variable's set { } add OnPropertyChanged("ColumnName");

    in ViewModel, public ObservableCollection<ClassName> ClassName {get; set;} in constructor of viewmodel class add ClassName = new ObservableCollection<ClassName>(); and return this class.

    Datacontext = viewmodel instance


    Hello
    Saturday, May 21, 2011 3:32 AM
  • Hi TasaHammer,

    I think this is a threading issue. If you do the work in the UIThread, then the UI probably don't get a change to update until your work is done.

    If you still have any questions or concerns about this issue, please feel free to let me know.

    I suggest you to do the work in a worker thread to keep the UI responsive. Please see Build More Responsive Apps With The Dispatcher for more information.

    Best regards,


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 24, 2011 6:35 AM
  • Min, thank you for your feedback.  If I am using UIThread it's happening without my knowledge.  I'm not making any UIThread references directly.  I think everything is happening in the same thread though.  I am not taking any steps to make this a multi-threaded process.  Let's say there are 10 rows in my listview, each row corresponding to a particular task.  I run something for each row (and it could be something as simple as a "sleep" call just to take up a few seconds in order to provide the visual delay to see if the update is happening one by one, a few seconds apart as one would expect).  The processing is done synchronously.  One row at a time.  The next row doesn't get started until the previous one is finished.  I try to update the second column of each row when that row's processing is done.  The udpate does not show when I expect them.  All rows are updated at the same time at the very end when all rows have completed.
    Thursday, May 26, 2011 6:42 PM
  • Hi TasaHammer,

    That same thread is in fact the UIThread. So the UI won't update until all work completes.

    If you only want to display the update on the UI, you can simply add the following code to the exact point when you want the UI to update.

    this.Dispatcher.Invoke(new Action(() => { }), System.Windows.Threading.DispatcherPriority.Background);
    

    The UI will still freeze but at least the changes will be reflected on the UI.

     

    Hope this helps.

    Best regards,

     


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by TasaHammer Friday, May 27, 2011 3:54 AM
    Friday, May 27, 2011 2:51 AM
  • Min,

    Bingo!  That made it so easy, and it is indeed what I want.  The UI works exactly the way it is supposed to.   Many many thanks!

    Tasa

    Friday, May 27, 2011 3:54 AM