完善下载机制

This commit is contained in:
zsyg
2025-07-27 08:16:32 +08:00
committed by GitHub
parent 94fafdb3ed
commit 76bb4d1b50
2 changed files with 523 additions and 508 deletions

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.Windows.Forms; using System.Windows.Forms;
@@ -14,6 +15,7 @@ namespace AppStore
public string FileName { get; set; } = string.Empty; public string FileName { get; set; } = string.Empty;
public int Progress { get; set; } public int Progress { get; set; }
public string Status { get; set; } = string.Empty; public string Status { get; set; } = string.Empty;
public Process? DownloadProcess { get; set; }
public DownloadItem() public DownloadItem()
{ {
@@ -123,9 +125,26 @@ namespace AppStore
try try
{ {
// 1. 先取消下载
DownloadManager.Instance.CancelDownload(this); DownloadManager.Instance.CancelDownload(this);
// 2. 更新状态为已取消
Status = "已取消"; Status = "已取消";
UpdateDisplay(); UpdateDisplay();
// 3. 延迟100ms后移除控件确保UI更新完成
var timer = new System.Windows.Forms.Timer { Interval = 100 };
timer.Tick += (s, args) =>
{
timer.Stop();
timer.Dispose();
if (this.Parent != null)
{
this.Parent.Controls.Remove(this);
this.Dispose();
}
};
timer.Start();
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@@ -24,7 +24,6 @@ namespace AppStore
private static DownloadManager instance = null!; private static DownloadManager instance = null!;
public static DownloadManager Instance => instance ??= new DownloadManager(); public static DownloadManager Instance => instance ??= new DownloadManager();
private Process? currentProcess;
public List<DownloadItem> DownloadItems { get; } = new List<DownloadItem>(); public List<DownloadItem> DownloadItems { get; } = new List<DownloadItem>();
public event Action<DownloadItem> DownloadAdded = delegate { }; public event Action<DownloadItem> DownloadAdded = delegate { };
@@ -78,6 +77,8 @@ namespace AppStore
Status = "准备下载" Status = "准备下载"
}; };
// 创建进程并关联到下载项
downloadItem.DownloadProcess = new Process();
DownloadItems.Add(downloadItem); DownloadItems.Add(downloadItem);
DownloadAdded?.Invoke(downloadItem); DownloadAdded?.Invoke(downloadItem);
@@ -137,9 +138,7 @@ namespace AppStore
var arguments = $"--out=\"{originalFileName}\" --dir=\"{downloadsDir}\" --split=16 --max-connection-per-server=16 {url}"; var arguments = $"--out=\"{originalFileName}\" --dir=\"{downloadsDir}\" --split=16 --max-connection-per-server=16 {url}";
currentProcess = new Process downloadItem.DownloadProcess.StartInfo = new ProcessStartInfo
{
StartInfo = new ProcessStartInfo
{ {
FileName = aria2cPath, FileName = aria2cPath,
Arguments = arguments, Arguments = arguments,
@@ -148,7 +147,6 @@ namespace AppStore
CreateNoWindow = true, CreateNoWindow = true,
RedirectStandardOutput = true, RedirectStandardOutput = true,
RedirectStandardError = true RedirectStandardError = true
}
}; };
// 获取目标文件路径 // 获取目标文件路径
@@ -170,7 +168,7 @@ namespace AppStore
} }
}; };
currentProcess.OutputDataReceived += (sender, e) => downloadItem.DownloadProcess.OutputDataReceived += (sender, e) =>
{ {
if (!string.IsNullOrEmpty(e.Data)) if (!string.IsNullOrEmpty(e.Data))
{ {
@@ -211,7 +209,7 @@ namespace AppStore
} }
}; };
currentProcess.ErrorDataReceived += (sender, e) => downloadItem.DownloadProcess.ErrorDataReceived += (sender, e) =>
{ {
if (!string.IsNullOrEmpty(e.Data)) if (!string.IsNullOrEmpty(e.Data))
{ {
@@ -221,9 +219,9 @@ namespace AppStore
} }
}; };
currentProcess.Exited += (sender, e) => downloadItem.DownloadProcess.Exited += (sender, e) =>
{ {
var process = currentProcess; var process = downloadItem.DownloadProcess;
if (process == null) return; if (process == null) return;
var result = GetProcessResult(process); var result = GetProcessResult(process);
@@ -292,10 +290,8 @@ namespace AppStore
} }
finally finally
{ {
if (process != null) // 清理资源
{ downloadItem.DownloadProcess = null;
currentProcess = null;
}
} }
// 强制更新显示 // 强制更新显示
@@ -304,13 +300,13 @@ namespace AppStore
if (!currentProcess.Start()) if (!downloadItem.DownloadProcess.Start())
{ {
throw new Exception("进程启动失败"); throw new Exception("进程启动失败");
} }
currentProcess.BeginOutputReadLine(); downloadItem.DownloadProcess.BeginOutputReadLine();
currentProcess.BeginErrorReadLine(); downloadItem.DownloadProcess.BeginErrorReadLine();
progressTimer.Start(); progressTimer.Start();
} }
catch (Exception ex) catch (Exception ex)
@@ -333,7 +329,7 @@ namespace AppStore
{ {
try try
{ {
var process = currentProcess; var process = item.DownloadProcess;
if (process?.StartInfo == null || process.HasExited) if (process?.StartInfo == null || process.HasExited)
{ {
item.Status = "已取消"; item.Status = "已取消";
@@ -343,7 +339,7 @@ namespace AppStore
process.Kill(); process.Kill();
process.Dispose(); process.Dispose();
currentProcess = null; item.DownloadProcess = null;
item.Status = "已取消"; item.Status = "已取消";
DownloadProgressChanged?.Invoke(item); DownloadProgressChanged?.Invoke(item);