I have an async lambda running on a DispatcherTimer to get data from a web API. My code is as follows:
_timelineSeekTimer = new DispatcherTimer();
_timelineSeekTimer.Interval = TimeSpan.FromMilliseconds(500);
_timelineSeekTimer.Tick += async (e, o) =>
{
var sw = new Stopwatch();
sw.Start();
Console.WriteLine("Init: " + sw.ElapsedMilliseconds);
// Get HttpResponseMessage from VADAAR API
var response = await VadaarExtensions.CenPostAsync("/viewers/" + Viewer.GetName() + "/canvases/default/renderers/01/time", "GETPAYLOAD");
Console.WriteLine("Response Time: " + sw.ElapsedMilliseconds);
// Extract JSON string from response
var responseBodyAsText = await response.Content.ReadAsStringAsync();
// Extract time value from JSON with regex @TODO: Not use regex
var parseJsonTime = Regex.Match(responseBodyAsText, @"(\d+)}").Groups[1].Value;
ulong timeUs = ulong.Parse(parseJsonTime);
TimelineSec = timeUs * 1e-6;
Console.WriteLine("Current Seek Time: " + TimelineSec);
};
My problem is that the faster the timer runs, the function starts to fail. Here is a sample output of the function:
Init: 0
Response Time: 14
Current Seek Time: 0.543245
<snip>
Init: 0
Response Time: 18
Current Seek Time: 8.668584
Init: 0
Init: 0
Response Time: 887
Current Seek Time: 10.553811
<snip>
Init: 0
Response Time: 885
Current Seek Time: 18.67975
Init: 0
Init: 0
Response Time: 1760
Current Seek Time: 20.56805
When using the browser-based GUI for the API, the response time is somewhere between 15-30ms. What is the explanation for these failures, and why does the response time get worse with each one?