C#C
C#17mo ago
Qwerz

✅ How do you handle countdowns of objects?

I have a List of Objects, each has an expiry date. I would like to display a countdown (e.g. 1 hour and 1 minute until expiry) in one of my views (WPF).

I'm unsure however, how to construct this in regards to timers. I calculate the time left by subtracting the current date from the date of expiration. If I do this in the object class and implement OnPropertyChanged I get a stackoverflow error, which is understandable because the subtraction would be calculated infinitely often. I also don't want to create a timer for every object because there could be quite a lot of those.

What is a better approach for this? I thought of creating a second collection that stores all the calculated values and is updated by a single timer, but I'm unsure how to implement this. I'd be grateful for your assistance!
Was this page helpful?