I have developed a live dashboard showing stream sensor data across the US. I use a System.Windows.Forms.Timer to activate reading the sensors every hour. Here is some code:
public void SensorTimerControl()
{
int microSecsPerHour = 1000 * 60 * 60;
_sensorUpdatesTimer = new System.Windows.Forms.Timer();
_sensorUpdatesTimer.Interval = (1 * microSecsPerHour) ;//Set the time interval here to 1 hour
_sensorUpdatesTimer.Enabled = true;
_sensorUpdatesTimer.Tick += SensorTimer_TickAsync;
}
private async void SensorTimer_TickAsync(object sender, EventArgs e)
{
//Read all ditch and reservoir sensors
UpdateAllDitchAndReservoirSensors();
//Update the other programs
WSMMainWindow.UpdateProgramsWithSensorData(ReservoirList);
}
My programs are collecting the data as expected, however, to refresh the screen/canvas in which the sensor graphs are drawn I have to move the mouse. How do I get the canvas to update automatically without the user moving the mouse? The canvas is recreated every time (the code that draws the canvas reconstructs the grid and the canvas every time).
I read various threads on this: WPF forcing redraw of canvas and C# wpf redrawing canvas depeding on Timer event which suggest using a DispatcherTimer. So I changed my code to this:
private DispatcherTimer _sensorUpdatesTimer = null;
public void SensorTimerControl()
{
_sensorUpdatesTimer = new DispatcherTimer();
_sensorUpdatesTimer.Interval = TimeSpan.FromSeconds(1);//Set the time interval here
_sensorUpdatesTimer.Tick += SensorTimer_TickAsync;
}
private async void SensorTimer_TickAsync(object sender, EventArgs e)
{
//Read all ditch and reservoir sensors
UpdateAllDitchAndReservoirSensors();
//Update the other programs
WSMMainWindow.UpdateProgramsWithSensorData(ReservoirList);
}
I set the timer interval to 1 second to test the code, but now the SensorTimer_TickAsync is not invoked. What did I do wrong?