110,545
社区成员
发帖
与我相关
我的任务
分享
private async void PART_FiltrationButton_Click(object sender, RoutedEventArgs e)
{
if (cts == null)
await DoWork((cts = new CancellationTokenSource()).Token, progress);
}
private async Task DoWork(CancellationToken token, IProgress<ProgressPartialResult> progress)
{
int j = 0;
for (int m = 0; m < 8000000; m++)
{
Task mytask = Task.Run(() =>
{
j++;
});
await mytask;
if (progress != null)
{
progress.Report(new ProgressPartialResult() { Current = m + 1, Total = 8000000 });
}
if (token.IsCancellationRequested)
{
//如果取消操作
progress.Report(new ProgressPartialResult() { Current = 0, Total = 8000000 });
}
}
}
private void Progress_ProgressChanged(object sender, ProgressPartialResult e)
{
progressBar1.Value = ((double)e.Current / e.Total) * 100;
}
for (int m = 0; m < 8000000; m++)
{
j++;
if (progress != null)
{
progress.AsyncReport(m +8000000 );
}
}
private async Task DoWork(CancellationToken token, IProgress<ProgressPartialResult> progress)
{
int j = 0;
for (int m = 0; m < 8000000; m++)
{
Task mytask = Task.Run(() =>
{
j++;
});
await mytask;
}
}