Compare commits

...

2 commits

Author SHA1 Message Date
jiuqianyuan
fe945a9698
Merge 9137bd0924 into 693afe3560 2025-11-24 15:59:40 +08:00
jiuqianyuan
9137bd0924 Fix: download to fast, speed displayed as 0. 2025-11-24 15:59:01 +08:00

View file

@ -71,28 +71,25 @@ public class DownloaderHelper
} }
}; };
var totalDatetime = DateTime.Now; var lastUpdateTime = DateTime.Now;
var totalSecond = 0;
var hasValue = false; var hasValue = false;
double maxSpeed = 0; double maxSpeed = 0;
await using var downloader = new Downloader.DownloadService(downloadOpt); await using var downloader = new Downloader.DownloadService(downloadOpt);
//downloader.DownloadStarted += (sender, value) =>
//{
// if (progress != null)
// {
// progress.Report("Start download data...");
// }
//};
downloader.DownloadProgressChanged += (sender, value) => downloader.DownloadProgressChanged += (sender, value) =>
{ {
var ts = DateTime.Now - totalDatetime; if (progress != null && value.BytesPerSecondSpeed > 0)
if (progress != null && ts.Seconds > totalSecond)
{ {
hasValue = true; hasValue = true;
totalSecond = ts.Seconds;
if (value.BytesPerSecondSpeed > maxSpeed) if (value.BytesPerSecondSpeed > maxSpeed)
{ {
maxSpeed = value.BytesPerSecondSpeed; maxSpeed = value.BytesPerSecondSpeed;
}
var ts = DateTime.Now - lastUpdateTime;
if (ts.TotalMilliseconds >= 1000)
{
lastUpdateTime = DateTime.Now;
var speed = (maxSpeed / 1000 / 1000).ToString("#0.0"); var speed = (maxSpeed / 1000 / 1000).ToString("#0.0");
progress.Report(speed); progress.Report(speed);
} }
@ -102,10 +99,19 @@ public class DownloaderHelper
{ {
if (progress != null) if (progress != null)
{ {
if (!hasValue && value.Error != null) if (hasValue && maxSpeed > 0)
{
var finalSpeed = (maxSpeed / 1000 / 1000).ToString("#0.0");
progress.Report(finalSpeed);
}
else if (value.Error != null)
{ {
progress.Report(value.Error?.Message); progress.Report(value.Error?.Message);
} }
else
{
progress.Report("0");
}
} }
}; };
//progress.Report("......"); //progress.Report("......");