前端
<!-- 透明的覆盖层,显示在页面上方,包含进度条 -->
<Grid Visibility="{Binding IsLoading}"
Background="Transparent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ZIndex="1">
<Grid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding CancelTaskCommand}" />
</Grid.InputBindings>
<Border Background="Black" Opacity="0.5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<!-- 使用 StackPanel 垂直排列进度条和文本 -->
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0, 50, 0, 0">
<!-- 进度条居中显示 -->
<ProgressBar x:Name="LoadingProgressBar"
IsIndeterminate="True"
Height="50"
Width="200"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="0, 0, 0, 0"
MouseDoubleClick="LoadingProgressBar_MouseDoubleClick"/>
<!-- 显示的文字 -->
<TextBlock Text="双击屏幕取消查询"
HorizontalAlignment="Center"
Foreground="White"
FontSize="30"
VerticalAlignment="Center"/>
</StackPanel>
</Border>
</Grid>
后端代码
private void LoadingProgressBar_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
viewModel.CancelTaskCommand.Execute(null);
}
viewModel代码
[RelayCommand]
private async void SelectInfo()
{
try
{
string sql = "..........";
ChangeLoadingStatus(true);//进入加载状态
// 创建 CancellationTokenSource,并获取 CancellationToken
_cancellationTokenSource = new CancellationTokenSource();
var token = _cancellationTokenSource.Token;
// 启动异步任务,并传递 token
List<ToolStr50> result = await Task.Run(() => ExecuteQueryWithCancellation(sql, token), token);
ChangeLoadingStatus(false);//结束加载状态
TaxinclusiveSum = "合计含税金额:0";
InfoCount = "数据汇总:0条";
}
catch (OperationCanceledException ex)
{
ChangeLoadingStatus(false);//结束加载状态
Notification.Error(ex.Message, millisecondTimeOut: 2200);
}
catch (Exception ex)
{
ChangeLoadingStatus(false);//结束加载状态
Notification.Error("系统异常-" + ex.Message, millisecondTimeOut: 2200);
}
}
private async Task<List<ToolStr50>> ExecuteQueryWithCancellation(string sql, CancellationToken token)
{
try
{
// 直接传递 CancellationToken 到数据库查询
return await dal.db.SqlQueryable<ToolStr50>(sql)
.ToListAsync(token); // 将 token 传递给 ToListAsync
}
catch (OperationCanceledException)
{
// 如果任务被取消,处理取消逻辑
throw new OperationCanceledException("查询已取消", null, token);
}
}
[RelayCommand]
// 取消任务的方法
public void CancelTask()
{
_cancellationTokenSource?.Cancel(); // 请求取消
}