mirror of
https://github.com/zs-yg/kortapp-z.git
synced 2025-12-06 08:00:44 +08:00
完善下载机制
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
|
||||
@@ -14,6 +15,7 @@ namespace AppStore
|
||||
public string FileName { get; set; } = string.Empty;
|
||||
public int Progress { get; set; }
|
||||
public string Status { get; set; } = string.Empty;
|
||||
public Process? DownloadProcess { get; set; }
|
||||
|
||||
public DownloadItem()
|
||||
{
|
||||
@@ -123,9 +125,26 @@ namespace AppStore
|
||||
|
||||
try
|
||||
{
|
||||
// 1. 先取消下载
|
||||
DownloadManager.Instance.CancelDownload(this);
|
||||
|
||||
// 2. 更新状态为已取消
|
||||
Status = "已取消";
|
||||
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)
|
||||
{
|
||||
|
||||
@@ -24,7 +24,6 @@ namespace AppStore
|
||||
private static DownloadManager instance = null!;
|
||||
public static DownloadManager Instance => instance ??= new DownloadManager();
|
||||
|
||||
private Process? currentProcess;
|
||||
public List<DownloadItem> DownloadItems { get; } = new List<DownloadItem>();
|
||||
|
||||
public event Action<DownloadItem> DownloadAdded = delegate { };
|
||||
@@ -78,6 +77,8 @@ namespace AppStore
|
||||
Status = "准备下载"
|
||||
};
|
||||
|
||||
// 创建进程并关联到下载项
|
||||
downloadItem.DownloadProcess = new Process();
|
||||
DownloadItems.Add(downloadItem);
|
||||
DownloadAdded?.Invoke(downloadItem);
|
||||
|
||||
@@ -137,9 +138,7 @@ namespace AppStore
|
||||
var arguments = $"--out=\"{originalFileName}\" --dir=\"{downloadsDir}\" --split=16 --max-connection-per-server=16 {url}";
|
||||
|
||||
|
||||
currentProcess = new Process
|
||||
{
|
||||
StartInfo = new ProcessStartInfo
|
||||
downloadItem.DownloadProcess.StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = aria2cPath,
|
||||
Arguments = arguments,
|
||||
@@ -148,7 +147,6 @@ namespace AppStore
|
||||
CreateNoWindow = true,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true
|
||||
}
|
||||
};
|
||||
|
||||
// 获取目标文件路径
|
||||
@@ -170,7 +168,7 @@ namespace AppStore
|
||||
}
|
||||
};
|
||||
|
||||
currentProcess.OutputDataReceived += (sender, e) =>
|
||||
downloadItem.DownloadProcess.OutputDataReceived += (sender, e) =>
|
||||
{
|
||||
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))
|
||||
{
|
||||
@@ -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;
|
||||
|
||||
var result = GetProcessResult(process);
|
||||
@@ -292,10 +290,8 @@ namespace AppStore
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (process != null)
|
||||
{
|
||||
currentProcess = null;
|
||||
}
|
||||
// 清理资源
|
||||
downloadItem.DownloadProcess = null;
|
||||
}
|
||||
|
||||
// 强制更新显示
|
||||
@@ -304,13 +300,13 @@ namespace AppStore
|
||||
|
||||
|
||||
|
||||
if (!currentProcess.Start())
|
||||
if (!downloadItem.DownloadProcess.Start())
|
||||
{
|
||||
throw new Exception("进程启动失败");
|
||||
}
|
||||
|
||||
currentProcess.BeginOutputReadLine();
|
||||
currentProcess.BeginErrorReadLine();
|
||||
downloadItem.DownloadProcess.BeginOutputReadLine();
|
||||
downloadItem.DownloadProcess.BeginErrorReadLine();
|
||||
progressTimer.Start();
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -333,7 +329,7 @@ namespace AppStore
|
||||
{
|
||||
try
|
||||
{
|
||||
var process = currentProcess;
|
||||
var process = item.DownloadProcess;
|
||||
if (process?.StartInfo == null || process.HasExited)
|
||||
{
|
||||
item.Status = "已取消";
|
||||
@@ -343,7 +339,7 @@ namespace AppStore
|
||||
|
||||
process.Kill();
|
||||
process.Dispose();
|
||||
currentProcess = null;
|
||||
item.DownloadProcess = null;
|
||||
|
||||
item.Status = "已取消";
|
||||
DownloadProgressChanged?.Invoke(item);
|
||||
|
||||
Reference in New Issue
Block a user