There is a queue of some atomic operations, in my case these are portions of data that are loaded into the database, it can also be, for example, a queue of files when copying. I want to calculate and deduce the rate of these operations, for example, so that at any moment I can see that 20 operations have been completed in the last minute or, for example, 120. I do not need super accuracy - a deviation of 2-5 units is considered acceptable. In parallel, even portions of the data can be added to the same queue from the outside, so it’s impossible to simply print the number of elements in the queue. How to implement it?
While typing the question, there was about this solution:
using System; using System.ComponentModel; using System.Linq; using System.Timers; class Program : INotifyPropertyChanged { static void Main(string[] args) => new Program().Run(); int count = 0; int[] tablet = new int[60]; public int Tempo => tablet.Sum(); public event PropertyChangedEventHandler PropertyChanged; void Snick() { ++count; } void Cut() { int s = DateTime.Now.Second; tablet[s] = count; count = 0; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Tempo))); } void Run() { Timer timer = new Timer(1000); timer.Elapsed += (s, e) => Cut(); timer.Start(); // ... while (true) { // ... Snick(); } timer.Stop(); } } But I still don’t fully like it, even if it introduces an array and a timer, if I suddenly want to output the rate in 1 s - the load from the timer can become significant, it’s also not clear what sections to break.
Maybe someone has a ready-made solution that is better?
DateTime.Nowfor counting is nonsense, get a static counter for a method call, timer to fix the value of the counter and reset it. - rdorn