Beschreibung:
Ich brauchte sowas in versch. Projekten deswegen hab ich mich mal dazu bewegen lassen endlich eine kleine Klasse zu schreiben... ist jetzt nichts bahnbrechendes aber kann ja sein das es jmnd braucht.
using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Threading;
namespace NetworkStats
{
/// <summary>
/// Simple speed observing logic for NetworkInterfaces.
/// </summary>
public class SpeedMeter : IDisposable
{
private Timer tUpdate;
private int _maxbacklog;
private int _updateintervall;
private NetworkInterface _adapter;
private IPv4InterfaceStatistics _stats;
private bool _enabled;
// Backlogs
private List<long> log_bytesDown;
private List<long> log_bytesUp;
/// <summary>
/// Get the observered NetworkInterface.
/// </summary>
public NetworkInterface Adapter { get { return _adapter; } }
/// <summary>
/// SpeedMeter will call this event periodically depending on the UpdateIntervall.
/// </summary>
public event EventHandler Updated = null;
/// <summary>
/// Get the calculated Downloadspeed @ bytes per seconds.
/// </summary>
public float DownloadSpeed { get; private set; }
/// <summary>
/// Get the calculated Uploadspeed @ bytes per seconds.
/// </summary>
public float UploadSpeed { get; private set; }
/// <summary>
/// Get or Set the maximum number of backlogs to calculate the average speeds.
/// </summary>
public int MaximumBacklog
{
get { return _maxbacklog; }
set
{
_reset();
_maxbacklog = value;
}
}
/// <summary>
/// Get or Set the time in milliseconds to wait until a new update.
/// </summary>
public int UpdateIntervall
{
get { return _updateintervall; }
set
{
_updateintervall = value;
_reset();
tUpdate.Change(0, _updateintervall);
}
}
/// <summary>
/// Enable / Disable this instance of SpeedMeter.
/// </summary>
public bool Enabled
{
get { return _enabled; }
set
{
_enabled = value;
_reset();
tUpdate.Change(0, _enabled ? _updateintervall : 0);
}
}
/// <summary>
/// Creates a instance of SpeedMeter.
/// </summary>
/// <param name="adapter"> The NetworkInterface to observe. </param>
public SpeedMeter(NetworkInterface adapter)
{
if (adapter == null) throw new ArgumentNullException("adapter");
_adapter = adapter;
log_bytesDown = new List<long>();
log_bytesUp = new List<long>();
_maxbacklog = 10;
_updateintervall = 500;
_enabled = true;
tUpdate = new Timer(new TimerCallback(_tUpdateCallback));
tUpdate.Change(0, _updateintervall);
}
private void _reset()
{
log_bytesUp.Clear();
log_bytesDown.Clear();
}
private void _tUpdateCallback(object parm)
{
if (_adapter.OperationalStatus == OperationalStatus.Up)
{
// get statistics
_stats = _adapter.GetIPv4Statistics();
// calc. download
DownloadSpeed = _calculateKbps(ref log_bytesDown, _maxbacklog,
_updateintervall, _stats.BytesReceived);
// calc. upload
UploadSpeed = _calculateKbps(ref log_bytesUp, _maxbacklog,
_updateintervall, _stats.BytesSent);
}
else
{
DownloadSpeed = 0;
UploadSpeed = 0;
}
if (Updated != null)
{
System.Windows.Forms.Control target =
Updated.Target as System.Windows.Forms.Control;
if (target != null && target.InvokeRequired)
target.Invoke(Updated, this, null);
else
Updated.Invoke(this, null);
}
}
private float _calculateKbps(ref List<long> bytelog,
int maxbacklog, int updateintervall, long bytes)
{
float kbps = 0;
bytelog.Insert(0, bytes);
if (bytelog.Count > maxbacklog) bytelog.RemoveAt(maxbacklog);
if (bytelog.Count > 1)
{
for (int i = 0; i < bytelog.Count - 1; i++)
{
kbps += (bytelog[i] - bytelog[i + 1]);
}
kbps /= bytelog.Count;
kbps *= (1000 / updateintervall);
}
return kbps;
}
#region IDisposable Member
public void Dispose()
{
_reset();
tUpdate.Dispose();
}
#endregion
#region Static
/// <summary>
/// Get the first busy NetworkInterface.
/// </summary>
/// <returns> The first busy NetworkInterface. </returns>
public static NetworkInterface GetBusyAdapter()
{
foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces())
{
IPv4InterfaceStatistics stats = adapter.GetIPv4Statistics();
if (stats.BytesReceived > 0)
return adapter;
}
return null;
}
#endregion
}
}
Schlagwörter: kbps, Netzwerkgeschwindigkeit, Netzwerkspeed, Speed, Netzwerk, Geschwindigkeit
Hi, in deiner Methode "_calculateKbps" gibt es einen Fehler: In der for-Schleife müssen die Werte summiert werden. In der Originalversion wurden die Werte immer wieder überschrieben und die Berechnung der kbps lieferte falsche Werte...
Hier der m.E. korrekte Code:
private float _calculateKbps(ref List<long> bytelog,
int maxbacklog, int updateintervall, long bytes)
{
float kbps = 0;
bytelog.Insert(0, bytes);
if (bytelog.Count > maxbacklog) bytelog.RemoveAt(maxbacklog);
if (bytelog.Count > 1)
{
for (int i = 0; i < bytelog.Count - 1; i++)
{
kbps += (bytelog[i] - bytelog[i + 1]);
}
kbps /= bytelog.Count;
kbps *= (1000 / updateintervall);
}
return kbps;
}
Oh, ja natürlich! Komisch bei mir Daheim ist der Code richtig k.A. was passiert ist ^^