Reputation: 3651
I was initially using ObservableClass to store my CPU Usage information every second and move this information to a chart which was working but kept adding on to memory.
Got some advice and moved to opt for a Queue class to be able to remove the information after a certain period has passed. But unlike an observable class, I can't store 2 arguments.
Should I use both queue and observable class or queue class is enough for my issue.
Initial codes using Observable class
class CPUClass{
ObservableCollection<KeyValuePair<double, double>> cpuChartList = new ObservableCollection<KeyValuePair<double, double>>();
//this method is fired off every second
private void timerChange(){
counter += 1;
//cpuCurrent is the current cpu usage value every second
cpuChartList.Add(new KeyValuePair<double, double>(counter, cpuCurrent));
}
}
//On the MainWindow
cpulineChart.DataContext = CPUClass.cpuChartList;
Trying with Queue Class
class CPUClass{
Queue queueCPU = new Queue();
//to hold past 30 seconds cpu usage information at any point
private void timerChange(){
counter += 1;
if (queueCPU.Count > 30)
{
queueCPU.Dequeue();
counter -= 1;
}
queueCPU.Enqueue(cpuCurrent);//
}
}
//On the MainWindow
cpulineChart.DataContext = CPUClass.queueCPU;
As you can see when using Queue Class, I am not able to include my counter to keep track of seconds on the chart. This is new to me thus could have messed up the whole concept. Also pondering if the way I am adding and removing the counter for the Queue Class way is messy. Please advice. Thank you.
Upvotes: -1
Views: 192
Reputation: 688
I have not used a queue here, but I think this is what you want to do. You want to update your observable collection every second and remove the oldest item stored in it. Based on this, I have provided the code below.
Hope this helps.
public class CPUClass
{
public ObservableCollection<KeyValuePair<double, double>> cpuChartList = new ObservableCollection<KeyValuePair<double, double>>();
private object _lock = new object();
private int _counter;
private int _Limit = 30; //max 30 secs of data
Timer _timer;
public CPUClass()
{
_timer = new Timer(1000);
_timer.Elapsed += _timer_Elapsed;
_timer.Enabled = true;
_counter = 0;
}
void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
App.Current.Dispatcher.Invoke(() => UpdateCollection());
}
void UpdateCollection()
{
lock (_lock)
{
_counter += 1;
//Get the CpuUsage
var cpuCurrent = GetCpuUsage();
//Remove the oldest item
if (cpuChartList.Count >= _Limit)
cpuChartList.RemoveAt(0);
cpuChartList.Add(new KeyValuePair<double, double>(_counter, cpuCurrent));
}
}
}
Upvotes: 1