From 96cfa4be48869e92ae54d9b9a7c99e42f79adf4d Mon Sep 17 00:00:00 2001
From: dong <1278815766@qq.com>
Date: Mon, 30 Jun 2025 23:54:52 +0800
Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=8D=A2=E6=9B=B4=E6=96=B0=E8=BF=9B?=
=?UTF-8?q?=E5=BA=A6=E6=98=BE=E7=A4=BA=E6=A0=B7=E5=BC=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Form1.cs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++----
README.md | 3 ++-
2 files changed, 52 insertions(+), 5 deletions(-)
diff --git a/Form1.cs b/Form1.cs
index 6d333db..7576e8f 100644
--- a/Form1.cs
+++ b/Form1.cs
@@ -75,6 +75,13 @@ namespace CheckDownload
// 基准目录路径(用于文件更新的目标目录)
private string _baseDirectory;
+ // === 新增: 用于显示整体下载大小与速度 ===
+ private long _totalDownloadedBytes = 0; // 已下载总字节数
+ private DateTime _downloadStartTime; // 下载开始时间
+ private DateTime _lastSpeedUpdateTime; // 上一次更新速度的时间
+ private readonly object _speedLock = new object(); // 锁,用于多线程更新 UI
+ private long _bytesSinceLastSpeedCalc = 0; // 距离上次速度计算新增的字节数
+
///
/// 初始化窗体
///
@@ -270,6 +277,13 @@ namespace CheckDownload
_totalCount = orderedFileList.Count;
_completedCount = 0;
_downloadedFiles.Clear();
+
+ // === 新增: 初始化速度统计 ===
+ _totalDownloadedBytes = 0;
+ _downloadStartTime = DateTime.UtcNow;
+ _lastSpeedUpdateTime = _downloadStartTime;
+ _bytesSinceLastSpeedCalc = 0;
+
var failedFiles = new ConcurrentDictionary();
await PerformDownloads(orderedFileList, failedFiles);
@@ -1496,10 +1510,21 @@ namespace CheckDownload
{
await localStream.WriteAsync(buffer, 0, bytesRead);
totalRead += bytesRead;
- if (totalBytes.HasValue)
+
+ // === 新增: 统计整体下载量 ===
+ Interlocked.Add(ref _totalDownloadedBytes, bytesRead);
+ Interlocked.Add(ref _bytesSinceLastSpeedCalc, bytesRead);
+
+ // === 修改: 显示整体下载进度和速度(每 500ms 更新一次,避免频繁刷新) ===
+ if (DateTime.UtcNow - _lastSpeedUpdateTime > TimeSpan.FromMilliseconds(500))
{
- string progressText = $"{FormatBytes(totalRead)}/{FormatBytes(totalBytes.Value)}";
- UpdateSize(progressText);
+ lock (_speedLock)
+ {
+ if (DateTime.UtcNow - _lastSpeedUpdateTime > TimeSpan.FromMilliseconds(500))
+ {
+ UpdateOverallSize();
+ }
+ }
}
}
}
@@ -1521,7 +1546,7 @@ namespace CheckDownload
dblSByte = bytes / 1024.0;
}
}
- return $"{dblSByte:0.##}{suffixes[i]}";
+ return $"{dblSByte:0.0}{suffixes[i]}";
}
///
@@ -1715,5 +1740,26 @@ namespace CheckDownload
}
catch { }
}
+
+ // === 新增: 更新整体下载大小与速度 ===
+ private void UpdateOverallSize()
+ {
+ DateTime now = DateTime.UtcNow;
+ double intervalSeconds = (now - _lastSpeedUpdateTime).TotalSeconds;
+ if (intervalSeconds <= 0) intervalSeconds = 0.1; // 防止除零
+
+ // 读取并清零自上次计算以来的字节数
+ long intervalBytes = Interlocked.Exchange(ref _bytesSinceLastSpeedCalc, 0);
+
+ double bytesPerSec = intervalBytes / intervalSeconds;
+
+ long downloaded = Interlocked.Read(ref _totalDownloadedBytes);
+
+ string speedText = $"{FormatBytes((long)bytesPerSec)}/s";
+ string sizeText = $"{FormatBytes(downloaded)}({speedText})";
+
+ _lastSpeedUpdateTime = now;
+ UpdateSize(sizeText);
+ }
}
}
\ No newline at end of file
diff --git a/README.md b/README.md
index d0a9534..3087b40 100644
--- a/README.md
+++ b/README.md
@@ -32,7 +32,7 @@
## 用户界面
-- **实时进度显示**:通过进度条、已完成数量/总数、下载速度等信息,清晰地展示更新进度。
+- **实时进度显示**:通过进度条、已完成数量/总数以及 **已下载总量(实时速度)** 的形式(两者均保留一位小数,每 0.5 秒刷新),清晰地展示更新进度。
- **简洁的状态反馈**:界面只显示当前正在处理的文件名等核心信息,避免被冗长的日志刷屏。
- **友好的错误提示**:当发生严重错误时,会弹出简明扼要的错误信息窗口,而不是难以理解的完整堆栈跟踪。
- **自动定位与退出**:窗体启动时会自动停靠在屏幕右下角,更新完成后会自动关闭,对用户干扰极小。
@@ -174,6 +174,7 @@
## 📝 版本历史
### 最新版本特性
+- ✅ 新增"已下载总量 + 实时下载速度"显示,速度与大小均保留一位小数,避免大文件下载时 UI 停滞误判。
- ✅ 添加7z自动解压功能
- ✅ 支持解压后程序自动设置管理员权限
- ✅ 优化多线程下载性能