feat(app): 应用程序退出时增加清理逻辑

- 将DoStopAllTask方法从私有改为公共以支持外部调用
- 将_uploadCancellation字段从私有改为公共以便在应用退出时取消上传任务
- 在应用程序退出时添加取消上传任务和停止下载任务的清理操作
- 改进系统托盘图标的清理逻辑确保资源正确释放
- 修复RealTimeListen方法中的参数格式问题
dev2.0
一梦千年 2026-01-27 09:32:08 +08:00
parent e68391d54a
commit 318e73d058
3 changed files with 12 additions and 8 deletions

View File

@ -62,6 +62,10 @@ namespace HeBianGu.App.Disk
{
// 在应用程序退出时清理系统托盘图标
CleanupSystemTrayIcon();
//
ViewModelLocator.SendViewModel._uploadCancellation.Cancel();
ViewModelLocator.DownViewModel.DoStopAllTask();
Console.WriteLine("退出执行");
base.OnExit(e);
}
@ -73,22 +77,22 @@ namespace HeBianGu.App.Disk
if (_mainWindow != null)
{
// 通过反射访问LinkWindowBase中的NotifyIcon实例并清理
var notifyIconField = typeof(LinkWindowBase).GetField("_notifyIcon",
var notifyIconField = typeof(LinkWindowBase).GetField("_notifyIcon",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
if (notifyIconField != null)
{
var notifyIcon = notifyIconField.GetValue(_mainWindow);
if (notifyIcon != null)
{
var disposeMethod = notifyIcon.GetType().GetMethod("Dispose",
var disposeMethod = notifyIcon.GetType().GetMethod("Dispose",
System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
disposeMethod?.Invoke(notifyIcon, null);
}
}
// 同时尝试通过公共方法隐藏窗口
_mainWindow.Hide();
// 确保所有资源得到释放
System.Windows.Forms.Application.Exit();
}
@ -97,7 +101,7 @@ namespace HeBianGu.App.Disk
{
System.Diagnostics.Debug.WriteLine($"清理系统托盘图标时出错: {ex.Message}");
}
// 确保应用程序完全退出
Application.Current.Shutdown();
}
@ -267,7 +271,7 @@ namespace HeBianGu.App.Disk
});
Task.Run(async () =>
{
await minioService.RealTimeListen(bucketName, setting.SyncDir, ( fileName) =>
await minioService.RealTimeListen(bucketName, setting.SyncDir, (fileName) =>
{
//添加同步信息
syncModel.SyncProgressText = $"{fileName}";

View File

@ -222,7 +222,7 @@ public class DownViewModel : MvcViewModelBase
}
// 停止所有任务
private void DoStopAllTask()
public void DoStopAllTask()
{
_taskQueue.Clear();
foreach (var minioDownloadTask in RunningTasks)

View File

@ -532,7 +532,7 @@ namespace HeBianGu.App.Disk
private bool _isTimerRunning = false;
private object _timerLock = new object();
private Task _currentUploadTask = null; // 新增:当前上传任务
private CancellationTokenSource _uploadCancellation = new();
public CancellationTokenSource _uploadCancellation = new();
public int _uploadCount = 0;
public int _completeCount = 0;
private int regionCount = 0;