添加停止执行功能并更新相关命令和UI

This commit is contained in:
tian 2026-01-27 20:41:54 +08:00
parent 8a3d480a0a
commit eef2d6b01c
4 changed files with 41 additions and 16 deletions

View File

@ -23,6 +23,7 @@ namespace NavisworksTransport.Core
private readonly Queue<int> _queue;
private readonly object _queueLock = new object();
private BatchQueueItem _currentItem;
private bool _shouldStop; // 停止执行标志
public event EventHandler<BatchQueueItemEventArgs> ItemAdded;
public event EventHandler<BatchQueueItemEventArgs> 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
}
/// <summary>
/// 取消当前任务
/// 注意:批处理计算是同步的,无法中途取消。此方法仅为接口保留。
/// 请求停止执行
/// 执行完当前条目后停止
/// </summary>
public void StopExecution()
{
_shouldStop = true;
LogManager.Info("[批处理队列] 已设置停止标志,将在完成当前项后停止执行");
}
/// <summary>
/// 取消当前任务(保留接口兼容性,已废弃)
/// </summary>
public void CancelCurrentItem()
{
LogManager.Info("[批处理队列] 取消操作请求:批处理计算无法中途取消,请等待当前任务完成");
LogManager.Warning("[批处理队列] CancelCurrentItem() 已废弃,请使用 StopExecution() 代替");
}
/// <summary>

View File

@ -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));
}
});

View File

@ -92,9 +92,9 @@ namespace NavisworksTransport.UI.WPF.ViewModels
public bool CanExecuteQueue => !IsExecuting && HasPendingItems;
/// <summary>
/// 是否可以取消当前任务
/// 是否可以停止执行
/// </summary>
public bool CanCancelCurrent => IsExecuting;
public bool CanStopExecution => IsExecuting;
/// <summary>
/// 是否可以删除选中项
@ -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
}
/// <summary>
/// 取消当前任务
/// 停止执行
/// </summary>
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}");
}
}

View File

@ -52,8 +52,8 @@ NavisworksTransport 批处理队列管理页签视图 - 采用与其他页签一
<Button Content="执行队列"
Command="{Binding ExecuteQueueCommand}"
Style="{StaticResource ActionButtonStyle}"/>
<Button Content="取消当前"
Command="{Binding CancelCurrentCommand}"
<Button Content="停止执行"
Command="{Binding StopExecutionCommand}"
Style="{StaticResource ActionButtonStyle}"/>
<Button Content="删除选中"
Command="{Binding DeleteItemCommand}"