private void TrafficStatistics(int queueMaxSize) { TrafficPerSecond previous, current; while (true) { previous = trafficPerSecondQueue.Last(); current = new TrafficPerSecond(); current.inboundCounter = InboundCounter; current.outboundCounter = OutboundCounter; current.inboundIncreasement = current.inboundCounter - previous.inboundCounter; current.outboundIncreasement = current.outboundCounter - previous.outboundCounter; trafficPerSecondQueue.Enqueue(current); if (trafficPerSecondQueue.Count > queueMaxSize) { trafficPerSecondQueue.Dequeue(); } TrafficChanged?.Invoke(this, new EventArgs()); Thread.Sleep(1000); } }
private async Task TrafficStatistics(int repeatMS, CancellationToken ct) { try { TrafficPerSecond previous, current; while (true) { previous = trafficPerSecondQueue.Last(); current = new TrafficPerSecond { inboundCounter = InboundCounter, outboundCounter = OutboundCounter }; current.inboundIncreasement = current.inboundCounter - previous.inboundCounter; current.outboundIncreasement = current.outboundCounter - previous.outboundCounter; trafficPerSecondQueue.Enqueue(current); if (trafficPerSecondQueue.Count > TrafficPerSecondQueueMaxSize) { trafficPerSecondQueue.Dequeue(); } TrafficChanged?.Invoke(this, new EventArgs()); await Task.Delay(repeatMS, ct); if (ct.IsCancellationRequested) { return; } } } catch (TaskCanceledException) { // ignore when this task got canceled } }
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { UpdateTrafficList(); TrafficChanged?.Invoke(this, new EventArgs()); }