在c#中显示任务进度,最标准的做法是使用`system.windows.forms.progressbar`控件绑定到后台线程或异步任务。通过调用`backgroundworker.reportprogress`、`taskscheduler`配合`iprogress

如何在WinForms中使用BackgroundWorker实现进度报告
使用`BackgroundWorker`组件可以方便地在后台线程执行耗时操作并报告进度。你需要设置`WorkerReportsProgress`为true,然后在后台工作事件中调用`ReportProgress`方法,同时在`ProgressChanged`事件中更新`ProgressBar`的值。这种方法适用于需要频繁更新进度且逻辑相对简单的场景。

using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
namespace WinFormsProgressDemo
{
public partial class MainForm : Form
{
private BackgroundWorker worker = new BackgroundWorker();
private ProgressBar progressBar = new ProgressBar();
private Button startButton = new Button();
public MainForm()
{
progressBar.Dock = DockStyle.Top;
startButton.Text = "Start";
startButton.Click += StartButton_Click;
this.Controls.Add(progressBar);
this.Controls.Add(startButton);
// 配置 BackgroundWorker
worker.WorkerReportsProgress = true;
worker.DoWork += Worker_DoWork;
worker.ProgressChanged += Worker_ProgressChanged;
}
private void StartButton_Click(object sender, EventArgs e)
{
if (!worker.IsBusy)
{
worker.RunWorkerAsync();
}
}
private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
// 模拟耗时任务
for (int i = 0; i <= 100; i++)
{
// 检查是否请求取消
if (worker.CancellationPending)
{
e.Cancel = true;
return;
}
// 模拟工作延迟
Thread.Sleep(50);
// 报告进度
worker.ReportProgress(i);
}
}
private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// 此方法在UI线程上执行
progressBar.Value = e.ProgressPercentage;
}
}
}
如何在WPF中使用IProgress和async/await模式
在现代WPF应用中,推荐使用`async/await`模式结合`IProgress

using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace WpfProgressDemo
{
public partial class MainWindow : Window
{
private ProgressBar progressBar = new ProgressBar();
private Button startButton = new Button();
public MainWindow()
{
progressBar.Maximum = 100;
progressBar.HorizontalAlignment = HorizontalAlignment.Stretch;
progressBar.VerticalAlignment = VerticalAlignment.Top;
startButton.Content = "Start Task";
startButton.HorizontalAlignment = HorizontalAlignment.Center;
startButton.VerticalAlignment = VerticalAlignment.Center;
startButton.Click += async (s, e) => await StartLongRunningTaskAsync();
var stackPanel = new StackPanel();
stackPanel.Children.Add(progressBar);
stackPanel.Children.Add(startButton);
this.Content = stackPanel;
}
private async Task StartLongRunningTaskAsync()
{
startButton.IsEnabled = false;
// 创建 Progress<int> 实例,默认捕获当前SynchronizationContext
var progress = new Progress<int>(value =>
{
progressBar.Value = value;
});
try
{
await Task.Run(() => PerformWork(progress));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
startButton.IsEnabled = true;
progressBar.Value = 0;
}
}
private void PerformWork(IProgress<int> progress)
{
for (int i = 0; i <= 100; i++)
{
// 模拟工作
System.Threading.Thread.Sleep(50);
// 报告进度
progress?.Report(i);
}
}
}
}
当任务完成时如何重置或隐藏进度条状态
任务完成后,通常需要重置`ProgressBar`的状态以便下次使用,或者根据业务需求隐藏它。在`BackgroundWorker`的`RunWorkerCompleted`事件中或在`async/await`的`finally`块中,可以将`Value`重置为0,并将`Maximum`恢复原值。如果进度条是动态添加的,还可以将其`Visibility`设置为`Collapsed`。这确保了用户界面的整洁性和可重用性,避免残留的进度指示误导用户。


















