ImageStream 的进度条不显示从 0 到 1 的值

Progress bar for a ImageStream doesn't show values from 0 to 1

我的 ASP.NET 应用程序中有一个图像下载进度条。 在 while 循环中是以下代码,其中我将当前流中的字节长度除以预期流以获得所需的进度:

if (ExpectedStreamSize.HasValue && _configSize.HasValue)
{
    var expected = ExpectedStreamSize + _configSize.Value;
    var progress = _stream.ReadPosition / (float) expected;
    var limitedProgress = progress > 1 ? 1 : progress;

    var epsilon = 0.001;
    if (!_lastReportedProgress.HasValue || _lastReportedProgress.Value + epsilon < limitedProgress)
        _onProgressChanged?.Invoke(limitedProgress);

    _lastReportedProgress = limitedProgress;
    LogToFile(limitedProgress);     // Logged to see the values
}

但是,我期望值来自 0 to 1。所以从 0% 到 100%。 当我调试时,我得到以下值分配:

ExpectedStreamSize.HasValue = true
_configSize.HasValue = 1714

ExpectedStreamSize = 8700000     // 8.7 MB
_configSize.Value = 1714
--> expected: 8701714

_stream.ReadPosition = 1722
expected: 8701714   
--> progress = 0.000197892048

我记录了 limitedProgress 的 值,它们是以下值:

0,000197892
0,0001981219
0,0001983517
...
0,0004684135

但我需要从 0,00019xxxx 到 1,0 的值(以便达到 100%)。

有人可以向我解释我做错了什么以及如何获得正确的进度状态吗?

所以我自己找到了这个bug并修复了它。

lastReportedProgress = limitedProgress当然属于if循环中,否则就是错误的。

上面的方案应该可以,但是时间消耗会很极端,因为这里输出和查询的中间步骤太多了。 现在它可以工作了,日志也 returns 我想象的值:

0,001000033
0,002000066
...
0,9970638
0,998064
0,9990641

我还调整了上面的 float 值,使 if 循环中的第二个条件看起来更好。

private float _lastReportedProgress = 0;

// some code 

    if(...)
    {
        if (_lastReportedProgress + epsilon < limitedProgress)
    }