gpt4 book ai didi

c# - NetworkInterface.GetIPv4Statistics().BytesReceived - 它返回什么?

转载 作者:行者123 更新时间:2023-11-30 13:23:15 35 4
gpt4 key购买 nike

该特定字段返回什么?我想要每秒接收的字节数。我应该依赖这个吗?

最佳答案

我认为你可以这样使用它:

long beginValue = NetworkInterface.GetIPv4Statistics().BytesReceived;
DateTime beginTime = DateTime.Now;

// do something

long endValue = NetworkInterface.GetIPv4Statistics().BytesReceived;
DateTime endTime = DateTime.Now;

long recievedBytes = endValue - beginValue;
double totalSeconds = (endTime - beginTime).TotalSeconds;

var bytesPerSecond = recievedBytes / totalSeconds;

定期更新的代码片段

private object _lockObj;
private long bytesPerSecond = 0;
private Timer _refreshTimer = new Timer { Interval = 1000 };

// do in ctor or some init method
_refreshTimer.Tick += _refreshTimer_Tick;
_refreshTimer.Enabled = true;

private void _refreshTimer_Tick(object sender, EventArgs e)
{
ThreadPool.QueueUserItem(callback =>
{
long beginValue = NetworkInterface.GetIPv4Statistics().BytesReceived;
DateTime beginTime = DateTime.Now;

Thread.Sleep(1000);

long endValue = NetworkInterface.GetIPv4Statistics().BytesReceived;
DateTime endTime = DateTime.Now;

long recievedBytes = endValue - beginValue;
double totalSeconds = (endTime - beginTime).TotalSeconds;

lock(_lockObj)
{
bytesPerSecond = recievedBytes / totalSeconds;
}
});

}

您可以将其与一些跟踪结合起来以记录随着时间​​的推移接收到的字节数

关于c# - NetworkInterface.GetIPv4Statistics().BytesReceived - 它返回什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13579530/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com