So for a project of mine I need to fetch values from a piece of hardware every 100 ms. The fetching takes x amount of milliseconds. So what I'm looking for but cannot find is a way to make sure a for/while always takes 100 ms (or more).
So in pseudo code:
for (int i = 0; i < 100; i++) // Take measurements for 10 seconds
{
// Call some async method that sleeps for 100 ms
this.temperatureList.Add(this.HardwareManager.GetSensorValue(ESensor.Temperature)); // Takes 15 ms
this.temperatureList.Add(this.HardwareManager.GetSensorValue(ESensor.Pressure)); // Takes 30 ms
// await async method to finish
}
I have tried some async await things with this but I cannot seem to comprehend it. Anyone who can help me find a reliable way to get 10 seconds of measurements with an interval of 100 ms?
EDIT: I know that since I'm running C# on Windows, timing is not really a valid option. But this does not really matter in my question. My question is, how can I make sure the for loop takes at least(!!!) a 100 ms, while being as close as possible to 100 ms.