Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WPF Progress Bar in a separate Window
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows;
namespace WPFProgressBar
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ProgressBarWindow pbw = null;
public MainWindow()
{
InitializeComponent();
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
// Calls the method that does work of which the progress will be shown.
ShowProgress();
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// Notifying the progress bar window of the current progress.
pbw.UpdateProgress(e.ProgressPercentage);
}
void ShowProgress()
{
try
{
// Using background worker to asynchronously run work method.
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += ProcessLogsAsynch;
worker.ProgressChanged += worker_ProgressChanged;
worker.RunWorkerAsync();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ERROR");
}
}
private void ProcessLogsAsynch(object sender, DoWorkEventArgs e)
{
Dispatcher.Invoke(() =>
{
// Disabling parent window controls while the work is being done.
btnStart.IsEnabled = false;
// Launch the progress bar window using Show()
// Note: ShowDialog() wouldn't work as it waits for the child window to close.
pbw = new ProgressBarWindow();
pbw.Show();
});
for (int i = 1; i <= 100; i++)
{
// Simulates work being done
Thread.Sleep(100);
// Reports progress
(sender as BackgroundWorker).ReportProgress(i);
}
Dispatcher.Invoke(() =>
{
// Enables parent window controls
btnStart.IsEnabled = true;
});
}
}
}
<Window x:Class="WPFProgressBar.ProgressBarWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFProgressBar"
mc:Ignorable="d"
Title="ProgressBarWindow" Height="80" Width="400">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<ProgressBar Grid.Column="0" Grid.Row="0"
Name="pbLoad"
Visibility="Visible"
Margin="12, 4" Height="24"
Value="0" Minimum="0" Maximum="100"/>
<StackPanel Grid.Column="0" Grid.Row="0"
Orientation="Horizontal"
HorizontalAlignment="Center" VerticalAlignment="Center">
<!-- TextBlock displayed in the center of the Progress Bar -->
<!-- Indicates progress as a percentage -->
<!-- Binds 'Value' property of ProgressBar control to TextBlock Text -->
<TextBlock Name="tbProgress" Visibility="Visible"
Text="{Binding ElementName=pbLoad, Path=Value, StringFormat={}{0:0}%}"
HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
</Grid>
</Window>
using System.Windows;
namespace WPFProgressBar
{
/// <summary>
/// Interaction logic for ProgressBarWindow.xaml
/// </summary>
public partial class ProgressBarWindow : Window
{
public ProgressBarWindow()
{
InitializeComponent();
}
public void UpdateProgress(int percentage)
{
// When progress is reported, update the progress bar control.
pbLoad.Value = percentage;
// When progress reaches 100%, close the progress bar window.
if (percentage == 100)
{
Close();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.