I am having issues with all the possible current and outdated ways to implement a method that is continuously running in the background to obtain data which is then displayed by the UI.
Generally speaking I stumbled upon async/await
usage in this answer while also reading about various timers in this answer.
My scenario is the following: I want to read data every 250ms and update my UI based on the data obtained. A naive approach would be a very simple call like this:
while (true) {
// do stuff
Thread.Sleep(250);
}
This code would not be called every 250ms depending on how long the execution in the body actually took! Furthermore in contrast to the timer specific answer I do not want to wait until this method completes to call it again, hence if the method turns out to take too long for one tick I still want to call it every 250ms as exactly as possible and abort the previous tick execution if necessary.
Should I use a generic Timer
, a DispatcherTimer
, a Task
etc? Do you have further references and related questions which are up to date?