improve:自动更新

This commit is contained in:
2025-07-22 21:01:25 +08:00
parent f88acf1375
commit 156e8a2686
+19 -6
View File
@@ -528,7 +528,9 @@ namespace Ink_Canvas.Helpers
{ {
LogHelper.WriteLogToFile($"AutoUpdate | 正在尝试多线程下载: {fileUrl}"); LogHelper.WriteLogToFile($"AutoUpdate | 正在尝试多线程下载: {fileUrl}");
int maxRetry = 3; int maxRetry = 3;
int threadCount = 32; int[] threadOptions = new int[] { 32, 4 };
foreach (int threadCount in threadOptions)
{
long totalSize = await GetContentLength(fileUrl); long totalSize = await GetContentLength(fileUrl);
if (totalSize <= 0) if (totalSize <= 0)
{ {
@@ -537,8 +539,6 @@ namespace Ink_Canvas.Helpers
} }
int blockSize = (int)Math.Ceiling((double)totalSize / threadCount); int blockSize = (int)Math.Ceiling((double)totalSize / threadCount);
int blockCount = (int)Math.Ceiling((double)totalSize / blockSize); int blockCount = (int)Math.Ceiling((double)totalSize / blockSize);
// 这里不再定义BlockTask类,直接使用
var blockQueue = new System.Collections.Concurrent.ConcurrentQueue<BlockTask>(); var blockQueue = new System.Collections.Concurrent.ConcurrentQueue<BlockTask>();
var finishedBlocks = new System.Collections.Concurrent.ConcurrentDictionary<int, bool>(); var finishedBlocks = new System.Collections.Concurrent.ConcurrentDictionary<int, bool>();
long[] blockDownloaded = new long[blockCount]; long[] blockDownloaded = new long[blockCount];
@@ -581,7 +581,7 @@ namespace Ink_Canvas.Helpers
// 合并所有块进度 // 合并所有块进度
long totalDownloaded = blockDownloaded.Sum(); long totalDownloaded = blockDownloaded.Sum();
double percent = (double)totalDownloaded / totalSize * 100; double percent = (double)totalDownloaded / totalSize * 100;
progressCallback?.Invoke(percent, $"多线程下载中: {percent:F1}%"); progressCallback?.Invoke(percent, $"多线程下载中({threadCount}线程): {percent:F1}%");
} }
} }
} }
@@ -617,15 +617,25 @@ namespace Ink_Canvas.Helpers
await Task.WhenAll(tasks); await Task.WhenAll(tasks);
if (cts.IsCancellationRequested || finishedBlocks.Count != blockCount) if (cts.IsCancellationRequested || finishedBlocks.Count != blockCount)
{ {
progressCallback?.Invoke(0, "多线程下载失败"); progressCallback?.Invoke(0, $"多线程下载失败({threadCount}线程)");
// 清理分块文件 // 清理分块文件
for (int i = 0; i < blockCount; i++) for (int i = 0; i < blockCount; i++)
{ {
string tempPath = destinationPath + $".part{i}"; string tempPath = destinationPath + $".part{i}";
if (File.Exists(tempPath)) File.Delete(tempPath); if (File.Exists(tempPath)) File.Delete(tempPath);
} }
if (threadCount == threadOptions.Last())
{
// 已经是最后一次尝试
return false; return false;
} }
else
{
LogHelper.WriteLogToFile($"AutoUpdate | {threadCount}线程下载失败,尝试降级为{threadOptions.Last()}线程");
progressCallback?.Invoke(0, $"{threadCount}线程下载失败,尝试降级为{threadOptions.Last()}线程");
continue;
}
}
// 合并所有块 // 合并所有块
using (var output = new FileStream(destinationPath, FileMode.Create, FileAccess.Write, FileShare.None)) using (var output = new FileStream(destinationPath, FileMode.Create, FileAccess.Write, FileShare.None))
{ {
@@ -639,9 +649,12 @@ namespace Ink_Canvas.Helpers
File.Delete(tempPath); File.Delete(tempPath);
} }
} }
progressCallback?.Invoke(100, "多线程下载完成"); progressCallback?.Invoke(100, $"多线程下载完成({threadCount}线程)");
return true; return true;
} }
// 理论上不会到这里
return false;
}
// 获取文件总大小 // 获取文件总大小
private static async Task<long> GetContentLength(string fileUrl) private static async Task<long> GetContentLength(string fileUrl)