【问题标题】:Download speed for Open Hardware Monitor打开硬件监视器的下载速度
【发布时间】:2014-04-23 01:16:54
【问题描述】:

我正在对 Open Hardware Monitor 进行一些更改。我将添加网络适配器的下载和上传速度。但是当我计算下载速度时,我得到了错误的计算。

由于 OHM 中的自动更新,我无法使用计时器来计算正确的下载速度。 在此处的源代码中,您可以看到我如何计算下载速度(以 Mb/s 为单位)。

在我做的类的构造中:

IPv4InterfaceStatistics interfaceStats = netInterfaces.GetIPv4Statistics();

bytesSent = interfaceStats.BytesSent;
bytesReceived = interfaceStats.BytesReceived;

stopWatch = new Stopwatch();
stopWatch.Start();

当更新方法被调用时(在某些随机时间)我这样做:

IPv4InterfaceStatistics interfaceStats = netInterfaces.GetIPv4Statistics();

stopWatch.Stop();
long time = stopWatch.ElapsedMilliseconds;

if (time != 0)
{
    long bytes = interfaceStats.BytesSent;
    long bytesCalc = ((bytes - bytesSent)*8);

    usedDownloadSpeed.Value = ((bytesCalc / time) * 1000)/1024;
    bytesSent = bytes;                 
}

希望有人能看到我的问题吗?

添加截图

【问题讨论】:

  • 你是什么意思?当您划分两个整数类型(bytesCalc/time) 时,您会得到一个截断的整数结果,这可能是问题所在。
  • 不确定这是否相关,但一个 KB 是 1024 字节,一个 MB 是 1024 KB。我在你的计算中看到了 1000 和 1000000。
  • 我已经改变了我的计算方式为 double bytesCalc = (((double)b​​ytes - (double)b​​ytesSent)*8); usedDownloadSpeed.Value = (float)((double)(bytesCalc / time) * 1000);但它不起作用。 I 具有与旧计算相同的值。
  • 请不要在问题标题中包含有关所用语言的信息,除非没有它就没有意义。标记用于此目的。

标签: c# download-speed openhardwaremonitor


【解决方案1】:

我之前的代码存在一些转换问题。 现在我有了这个来源,它可以工作了。 全部回复。

    interfaceStats = netInterfaces.GetIPv4Statistics();

        //Calculate download speed
        downloadSpeed.Value = Convert.ToInt32(interfaceStats.BytesReceived - bytesPreviousReceived) / 1024F;

        bytesPreviousReceived = interfaceStats.BytesReceived;

【讨论】:

    【解决方案2】:

    以下更改应该会有所帮助...

    speed = netInterfaces.Speed / 1048576L;
    

    如果我没记错的话,Speed 属性是long,当你将它除以int 时,你最终会得到一个截断的结果。这使我们在您的其他计算中也发生了类似的变化......

    usedDownloadSpeed.Value = ((bytesCalc / time) * 1000L)/1024L;
    

    ...假设usedDownloadSpeed.Value 也是long,以确保您不会通过隐式转换结果或计算得到任何截断值。如果您想更加确定自己的投射正确,请使用Convert.ToInt64()。

    【讨论】:

    • usedDownloadSpeed.Value 是一个浮点数。所以我必须将 long 转换为浮点值。
    猜你喜欢
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多