How to display the progress of a program running in WPF?

In WPF, the ProgressBar control can be used to display the progress of the program’s execution.

First, add a ProgressBar control in the XAML file.

<ProgressBar x:Name="progressBar" Width="200" Height="20" Minimum="0" Maximum="100" Value="0"/>

Then, use DispatcherTimer in the code to update the progress of the ProgressBar.

using System.Windows;
using System.Windows.Threading;

public partial class MainWindow : Window
{
    private DispatcherTimer timer;
    private int progress;

    public MainWindow()
    {
        InitializeComponent();

        // 初始化进度为0
        progress = 0;

        // 创建一个DispatcherTimer,每隔一段时间更新进度
        timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(0.1);
        timer.Tick += Timer_Tick;
        timer.Start();
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        // 更新进度
        progress += 1;
        progressBar.Value = progress;

        // 当进度达到100时,停止计时器
        if (progress >= 100)
        {
            timer.Stop();
        }
    }
}

In the above code, we are using a DispatcherTimer to update the progress bar value every 0.1 seconds until the progress reaches 100, at which point the timer stops. In the Timer_Tick event handler, we increment the progress value and assign it to the ProgressBar’s Value property to update the progress bar display.

bannerAds