0

What is the best way to refresh a WPF ListView based on a EndTime property.

Reload the list every minute with a TimeDispatcher?

What I mean is I have a list with Meeting objects.

With Start and End time. When the EndTime is < DateTime.Now() the meeting object should be removed from the list, the UI needs to be updated then.

For example train arrival panels in stations.

Any suggestions/examples?

user7998549
  • 131
  • 3
  • 15
  • What does the ListView Show? What should be changed in case the EndTime property is reached? Is there any binding that should be updated? – Daniel W. Nov 13 '18 at 15:06
  • 1
    You may use a scheduler framework such as for example [FluentScheduler](https://github.com/fluentscheduler/FluentScheduler) or [Quartz.NET](https://www.quartz-scheduler.net/) to invoke an action at a specific time. – mm8 Nov 13 '18 at 15:13
  • @DanielW. Its a List with meeting objects List. and this list is binded to a ListView control. So the list shows a list of meetings with name and start and end time. and when the endtime of a meetings is < the current time it should be removed from the ListView (not be showed anymore to the users) – user7998549 Nov 13 '18 at 15:13
  • In that case you need only to modify the List, GUI should be updated automatically. For Updating the list use the hint from mm8. – Daniel W. Nov 13 '18 at 15:20
  • Ok, will look into that. – user7998549 Nov 13 '18 at 15:44

1 Answers1

1

Setting a timer in the GUI is only appropriate if you don't control that data - for example if you have to query a database table to see if someone else has changed it. You're updating your view of the data every minute.

If you control that data, the data should change and WPF should be able to notice when it changes. Usually this just means converting List<Meeting> to `Observable'.

You could either have a single timer that fires every minute, and check every meeting each time (or sort them and just check the first one)

Or you could create a timer for each Meeting, which is calculated to fire at the right time.

Or, if you've sorted the list, you could just set a single timer to fire when the first meeting occurs, and then work out what is next (and recalculate this if anything changes)

The first is probably most robust, but least efficient, while the last is the opposite.

Robin Bennett
  • 3,192
  • 1
  • 8
  • 18
  • That occurs when you are iterating through a collection and you change it inside the loop. You either need to jump out of the loop after adding/removing an item, or use another collection to track the changes you want to make, and them make them when the loop has finished. https://stackoverflow.com/questions/604831/collection-was-modified-enumeration-operation-may-not-execute – Robin Bennett Nov 14 '18 at 12:19