I've figured out how to store Data from a .csv to a List<>.
I've two Lists: ListA
which contains the timestamp for each value and ListB
which contains the values.
It looks like this:
A (time [ms]) | B (value)
---------------------------
0,00 | 49,33
154,71 | 49,46
244,92 | 49,72
855,11 | 49,64
...
And so on (over 50.000 values)
My actual issue is:
I want to send the values to the console using WriteLine(listA[i]);
at the (nearly) exact time like the timestamp of the actual value.
I thought about a stopwatch which i compare with the timestamps to give the right value out at the right moment?
Is this possible?
Update #1:
Here is my code. It works (kind of). But i'm not sure if there is a much better solution?
stopwatch.Start();
while(true)
{
for (int i = 0; i < 50000; i++)
{
if (Math.Abs(stopwatch.Elapsed.TotalMilliseconds - Convert.ToDouble(listA[i]) * 1000) < 10)
{
Console.WriteLine(listB[i]);
}
}
Thread.Sleep(5);
}
Update #2:
I'm trying to understand the solution of Mong Zhu @disclaimer. My target is to create a method which get initiated by a timer (every 100ms as example). I already have the code around this Problem is: Timestamp and Stopwatch are sometimes not synced and it skips some values.
Here is my method (called every 100ms):
public double getvalue()
{
if (stopwatch.ElapsedMilliseconds > Convert.ToDouble(ListA[ListA.Count-1])*1000)
{
stopwatch.Stop();
}
else
{
for (var i = 0; i < ListA.Count; i++)
{
if (Math.Abs(stopwatch.ElapsedMilliseconds - Convert.ToDouble(ListA[i]) * 1000) < 5)
{
value = Convert.ToDouble(ListB[i]);
break;
}
}
}
return value;
}