private static async Task Handle(NetworkSpeed item, Direction direction) { if (!NetworkInterface.GetIsNetworkAvailable()) { throw new Exception("There are no available network connections."); } var networkInterface = GetNetworkInterfaceById(item.NetworkInterfaceId); var startValue = networkInterface.GetIPv4Statistics(); var starTime = DateTime.Now; await Task.Delay(1000).ConfigureAwait(false); var endValue = networkInterface.GetIPv4Statistics(); var endTime = DateTime.Now; var totalBytes = direction == Direction.Upload ? endValue.BytesSent - startValue.BytesSent : endValue.BytesReceived - startValue.BytesReceived; var bitsPerSecond = (totalBytes * 8) / (endTime - starTime).TotalSeconds; if (bitsPerSecond > 1000000) { item.Value = Math.Round(bitsPerSecond / 1000000, 1); item.Symbol = " Mbps"; } else { item.Value = Math.Round(bitsPerSecond / 1000, 1); item.Symbol = " Kbps"; } item.State = State.Ok; }
private void Handle(NetworkSpeed item, Direction direction) { if (NetworkInterface.GetIsNetworkAvailable() == false) { throw new Exception("There are no available network connections."); } var networkInterface = GetNetworkInterfaceById(item.NetworkInterfaceId); if (networkInterface == null) { throw new Exception("The network interface was not found. Please select another network interface."); } var startValue = networkInterface.GetIPv4Statistics(); var starTime = DateTime.Now; Thread.Sleep(1000); var endValue = networkInterface.GetIPv4Statistics(); var endTime = DateTime.Now; long totalBytes; totalBytes = direction == Direction.Upload ? endValue.BytesSent - startValue.BytesSent : endValue.BytesReceived - startValue.BytesReceived; var bitsPerSecond = (totalBytes * 8) / (endTime - starTime).TotalSeconds; item.Value = bitsPerSecond > 1000000 ? Math.Round(bitsPerSecond / 1000000, 1) + " Mbps" : Math.Round(bitsPerSecond / 1000, 1) + " Kbps"; item.State = State.Ok; }
private static async Task Handle(NetworkSpeed widget, Direction direction) { if (!NetworkInterface.GetIsNetworkAvailable()) { throw new Exception("There are no available network interfaces."); } var networkInterface = GetNetworkInterfaceById(widget.NetworkInterfaceId); var startValue = networkInterface.GetIPv4Statistics(); var starTime = DateTime.Now; await Task.Delay(1000).ConfigureAwait(false); var endValue = networkInterface.GetIPv4Statistics(); var endTime = DateTime.Now; var totalBytes = direction == Direction.Upload ? endValue.BytesSent - startValue.BytesSent : endValue.BytesReceived - startValue.BytesReceived; widget.Value = totalBytes / (endTime - starTime).TotalSeconds; widget.State = State.Ok; }