private void WatchCPUAndMemory()
{
var running = true;
// Stop watching CPU and memory when the application stops or this control is removed
Application.Stopped.SubscribeForLifetime(() => { running = false; }, Application.LifetimeManager);
this.RemovedFromVisualTree.SubscribeForLifetime(() => { running = false; }, this.LifetimeManager);
while (running)
{
var now = DateTime.Now;
var cpuUsed = PerformanceInfo.GetCPUPercentage();
var memUsed = Math.Round(100 - (100.0 * PerformanceInfo.GetPhysicalAvailableMemoryInMiB() / PerformanceInfo.GetTotalMemoryInMiB()), 1);
// slide the window so it always shows the last minute
ViewModel.XMaximumOverride = now.Ticks;
ViewModel.XMinimumOverride = now.Ticks - TimeSpan.FromMinutes(1).Ticks;
// add the latest value to the series
ViewModel.DataSeriesCollection[0].DataPoints.Add(new DataPoint() { X = now.Ticks, Y = cpuUsed });
ViewModel.DataSeriesCollection[1].DataPoints.Add(new DataPoint() { X = now.Ticks, Y = memUsed });
// Remove the oldest data point if we have a minute worth of data on the chart
if (ViewModel.DataSeriesCollection[0].DataPoints.Count > 60)
{
ViewModel.DataSeriesCollection[0].DataPoints.RemoveAt(0);
ViewModel.DataSeriesCollection[1].DataPoints.RemoveAt(0);
}
Thread.Sleep(1000);
}
}