diff --git a/src/Core/BatchQueueManager.cs b/src/Core/BatchQueueManager.cs index 5130a61..95a32ae 100644 --- a/src/Core/BatchQueueManager.cs +++ b/src/Core/BatchQueueManager.cs @@ -23,6 +23,7 @@ namespace NavisworksTransport.Core private readonly Queue _queue; private readonly object _queueLock = new object(); private BatchQueueItem _currentItem; + private bool _shouldStop; // 停止执行标志 public event EventHandler ItemAdded; public event EventHandler ItemStarted; @@ -141,6 +142,13 @@ namespace NavisworksTransport.Core break; } + // 检查是否请求停止执行 + if (_shouldStop) + { + LogManager.Info("[批处理队列] 收到停止执行请求,将在完成当前项后停止"); + break; + } + int itemId; lock (_queueLock) { @@ -222,6 +230,9 @@ namespace NavisworksTransport.Core { Autodesk.Navisworks.Api.Application.EndProgress(); } + + // 重置停止标志 + _shouldStop = false; } } @@ -400,12 +411,21 @@ namespace NavisworksTransport.Core } /// - /// 取消当前任务 - /// 注意:批处理计算是同步的,无法中途取消。此方法仅为接口保留。 + /// 请求停止执行 + /// 执行完当前条目后停止 + /// + public void StopExecution() + { + _shouldStop = true; + LogManager.Info("[批处理队列] 已设置停止标志,将在完成当前项后停止执行"); + } + + /// + /// 取消当前任务(保留接口兼容性,已废弃) /// public void CancelCurrentItem() { - LogManager.Info("[批处理队列] 取消操作请求:批处理计算无法中途取消,请等待当前任务完成"); + LogManager.Warning("[批处理队列] CancelCurrentItem() 已废弃,请使用 StopExecution() 代替"); } /// diff --git a/src/Core/UIStateManager.cs b/src/Core/UIStateManager.cs index cc1afdd..6fe44f6 100644 --- a/src/Core/UIStateManager.cs +++ b/src/Core/UIStateManager.cs @@ -189,7 +189,12 @@ namespace NavisworksTransport.Core { if (!tcs.Task.IsCompleted) { - tcs.TrySetException(new TimeoutException($"UI操作超时({timeout}ms)")); + // 记录警告日志,但不抛出异常(批处理等长时间操作可能需要更长时间) + LogManager.Warning($"UI操作超时({timeout}ms),但操作可能仍在后台执行中"); + + // 对于长时间操作(如批处理),超时不应该是错误 + // 设置结果为默认值,让调用者决定如何处理 + tcs.TrySetResult(default(T)); } }); diff --git a/src/UI/WPF/ViewModels/BatchTaskManagementViewModel.cs b/src/UI/WPF/ViewModels/BatchTaskManagementViewModel.cs index eca3334..0e69cb4 100644 --- a/src/UI/WPF/ViewModels/BatchTaskManagementViewModel.cs +++ b/src/UI/WPF/ViewModels/BatchTaskManagementViewModel.cs @@ -92,9 +92,9 @@ namespace NavisworksTransport.UI.WPF.ViewModels public bool CanExecuteQueue => !IsExecuting && HasPendingItems; /// - /// 是否可以取消当前任务 + /// 是否可以停止执行 /// - public bool CanCancelCurrent => IsExecuting; + public bool CanStopExecution => IsExecuting; /// /// 是否可以删除选中项 @@ -119,7 +119,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels #region 命令 public ICommand ExecuteQueueCommand { get; } - public ICommand CancelCurrentCommand { get; } + public ICommand StopExecutionCommand { get; } public ICommand DeleteItemCommand { get; } public ICommand ViewReportCommand { get; } public ICommand RefreshCommand { get; } @@ -140,10 +140,10 @@ namespace NavisworksTransport.UI.WPF.ViewModels // 初始化命令 ExecuteQueueCommand = new RelayCommand(async () => await ExecuteQueueAsync(), () => CanExecuteQueue); - CancelCurrentCommand = new RelayCommand(CancelCurrent, () => CanCancelCurrent); + StopExecutionCommand = new RelayCommand(StopExecution, () => CanStopExecution); DeleteItemCommand = new RelayCommand(async () => await DeleteItemAsync(), () => CanDeleteItem); ViewReportCommand = new RelayCommand(async () => await ViewReportAsync(), () => CanViewReport); - RefreshCommand = new RelayCommand(async () => await LoadQueueItemsAsync(), () => !IsExecuting); + RefreshCommand = new RelayCommand(async () => await LoadQueueItemsAsync()); } catch (Exception ex) { @@ -261,21 +261,21 @@ namespace NavisworksTransport.UI.WPF.ViewModels } /// - /// 取消当前任务 + /// 停止执行 /// - private void CancelCurrent() + private void StopExecution() { try { if (_queueManager != null) { - _queueManager.CancelCurrentItem(); - LogManager.Info("[BatchQueueManagement] 已取消当前任务"); + _queueManager.StopExecution(); + LogManager.Info("[BatchQueueManagement] 已请求停止批处理执行"); } } catch (Exception ex) { - LogManager.Error($"[BatchQueueManagement] 取消任务失败: {ex.Message}"); + LogManager.Error($"[BatchQueueManagement] 停止执行失败: {ex.Message}"); } } diff --git a/src/UI/WPF/Views/BatchTaskManagementView.xaml b/src/UI/WPF/Views/BatchTaskManagementView.xaml index 6f6880c..f49ddfe 100644 --- a/src/UI/WPF/Views/BatchTaskManagementView.xaml +++ b/src/UI/WPF/Views/BatchTaskManagementView.xaml @@ -52,8 +52,8 @@ NavisworksTransport 批处理队列管理页签视图 - 采用与其他页签一