improve:自动更新
This commit is contained in:
@@ -528,7 +528,9 @@ namespace Ink_Canvas.Helpers
|
||||
{
|
||||
LogHelper.WriteLogToFile($"AutoUpdate | 正在尝试多线程下载: {fileUrl}");
|
||||
int maxRetry = 3;
|
||||
int threadCount = 32;
|
||||
int[] threadOptions = new int[] { 32, 4 };
|
||||
foreach (int threadCount in threadOptions)
|
||||
{
|
||||
long totalSize = await GetContentLength(fileUrl);
|
||||
if (totalSize <= 0)
|
||||
{
|
||||
@@ -537,8 +539,6 @@ namespace Ink_Canvas.Helpers
|
||||
}
|
||||
int blockSize = (int)Math.Ceiling((double)totalSize / threadCount);
|
||||
int blockCount = (int)Math.Ceiling((double)totalSize / blockSize);
|
||||
|
||||
// 这里不再定义BlockTask类,直接使用
|
||||
var blockQueue = new System.Collections.Concurrent.ConcurrentQueue<BlockTask>();
|
||||
var finishedBlocks = new System.Collections.Concurrent.ConcurrentDictionary<int, bool>();
|
||||
long[] blockDownloaded = new long[blockCount];
|
||||
@@ -581,7 +581,7 @@ namespace Ink_Canvas.Helpers
|
||||
// 合并所有块进度
|
||||
long totalDownloaded = blockDownloaded.Sum();
|
||||
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);
|
||||
if (cts.IsCancellationRequested || finishedBlocks.Count != blockCount)
|
||||
{
|
||||
progressCallback?.Invoke(0, "多线程下载失败");
|
||||
progressCallback?.Invoke(0, $"多线程下载失败({threadCount}线程)");
|
||||
// 清理分块文件
|
||||
for (int i = 0; i < blockCount; i++)
|
||||
{
|
||||
string tempPath = destinationPath + $".part{i}";
|
||||
if (File.Exists(tempPath)) File.Delete(tempPath);
|
||||
}
|
||||
if (threadCount == threadOptions.Last())
|
||||
{
|
||||
// 已经是最后一次尝试
|
||||
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))
|
||||
{
|
||||
@@ -639,9 +649,12 @@ namespace Ink_Canvas.Helpers
|
||||
File.Delete(tempPath);
|
||||
}
|
||||
}
|
||||
progressCallback?.Invoke(100, "多线程下载完成");
|
||||
progressCallback?.Invoke(100, $"多线程下载完成({threadCount}线程)");
|
||||
return true;
|
||||
}
|
||||
// 理论上不会到这里
|
||||
return false;
|
||||
}
|
||||
|
||||
// 获取文件总大小
|
||||
private static async Task<long> GetContentLength(string fileUrl)
|
||||
|
||||
Reference in New Issue
Block a user