In this article, we will see how to make use of the Progress Bar control provided in WPF. This control is especially used for show progress on automated operations e.g. File Copy, Installation etc. In this short article, I will explain how to make use of the Progress Bar control for simulating file copy operation from source to the destination path.
Step 1: Open VS2010 and create a WPF application. Name it as ‘WPF40_ProgressBar’.
Step 2: Open MainPage.Xaml and write the following Xaml code:
<Window x:Class="WPF40_ProgressBar.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="483" Width="719"
Loaded="Window_Loaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="82*" />
<RowDefinition Height="229*" />
</Grid.RowDefinitions>
<TextBlock Height="26" HorizontalAlignment="Left" Margin="18,10,0,0"
Name="txtStatus" Text="Copying....." VerticalAlignment="Top" Width="211" />
<ProgressBar Height="30" HorizontalAlignment="Left" Margin="14,36,0,0" Name="downloadProgress" VerticalAlignment="Top" Width="672" />
<Button Content="Copy Files" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="278,254,0,0" Name="btnCopy" VerticalAlignment="Top" Width="75" Click="btnCopy_Click" />
<ListBox Grid.Row="1" Height="202" HorizontalAlignment="Left" Margin="26,90,0,0" Name="lstSourceFiles" VerticalAlignment="Top" Width="180" />
<ListBox Height="202" HorizontalAlignment="Left" Margin="496,90,0,0" Name="lstTgrFiles" VerticalAlignment="Top" Width="180" Grid.Row="1" />
<TextBlock Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="66,51,0,0" Name="textBlock1" Text="Source Path" VerticalAlignment="Top" Width="89" />
<TextBlock Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="558,51,0,0" Name="textBlock2" Text="Target Path" VerticalAlignment="Top" Width="78" />
<Button Content="Source Path" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="278,15,0,0" Name="btnSourcePath" VerticalAlignment="Top" Width="75" Click="btnSourcePath_Click" />
<TextBlock Height="35" HorizontalAlignment="Left" Margin="14,72,0,0" Name="txtFileCopied" Text="" VerticalAlignment="Top" Width="224" />
</Grid>
</Window>
Note: We will be talking about the events on the controls when we come to the code. The following UI will be the generated.
The above UI defines ProgressBar(in Gray shade), ListBoxes represents Source Files to copy to Destination file path. The Button ‘Source Path’ displays all the files to copy in the ListBox below ‘Source Path’ TextBlock. When the user clicks on ‘Copy Files’ button, all the files will be copied in the ListBox below ‘Target Path’ TextBlock. This copy operations are simulated by the ‘Progress Bar’.
Step 3: Open MainWindow.Xaml.cs and write the following code:
Declare following variables at class leve (Change the drive name and folder as per your requirements):
C#
string SrcPath = @"H:\SrcFiles";
string TgtPath = @"H:\TgtFiles";
string[] Files;
string strFileName = "";
VB.NET (Converted Code)
Dim SrcPath As String = "H:\SrcFiles"
Dim TgtPath As String = "H:\TgtFiles"
Dim Files() As String
Dim strFileName As String = ""
Write the following code on the Loaded Event of the window:
C#
private void Window_Loaded(object sender, RoutedEventArgs e)
{
txtStatus.Visibility = System.Windows.Visibility.Hidden;
}
VB.NET (Converted Code)
Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
txtStatus.Visibility = System.Windows.Visibility.Hidden
End Sub
Code on ‘Source Path’ button:
C#
private void btnSourcePath_Click(object sender, RoutedEventArgs e)
{
string[] Files = Directory.GetFiles(SrcPath);
foreach (string s in Files)
{
lstSourceFiles.Items.Add(s);
}
downloadProgress.Maximum = Files.Length;
}
VB.NET (Converted Code)
Private Sub btnSourcePath_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim Files() As String = Directory.GetFiles(SrcPath)
For Each s As String In Files
lstSourceFiles.Items.Add(s)
Next s
downloadProgress.Maximum = Files.Length
End Sub
Write the following code on the ‘Copy Files’ button:
C#
private void btnCopy_Click(object sender, RoutedEventArgs e)
{
Files = Directory.GetFiles(SrcPath);
txtStatus.Visibility = System.Windows.Visibility.Visible;
DispatcherTimer timer = new DispatcherTimer();
foreach (string s in Files)
{
Thread t = new Thread(
new ThreadStart(
delegate()
{
DispatcherOperation dispOp =
downloadProgress.Dispatcher.BeginInvoke(DispatcherPriority.Loaded,
new Action(
delegate()
{
strFileName = s;
string fileName = System.IO.Path.GetFileName(strFileName);
string destFile = System.IO.Path.Combine(TgtPath, fileName);
System.IO.File.Copy(strFileName, destFile, true);
lstTgrFiles.Items.Add(destFile);
downloadProgress.Value = lstTgrFiles.Items.Count;
txtFileCopied.Text = lstTgrFiles.Items.Count + " File(s) Copied.";
Thread.Sleep(100);
}
));
dispOp.Completed += new EventHandler(dispOp_Completed);
}
));
t.Start();
}
}
VB.NET (Converted Code)
Private Sub btnCopy_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
Files = Directory.GetFiles(SrcPath)
txtStatus.Visibility = System.Windows.Visibility.Visible
Dim timer As New DispatcherTimer()
For Each s As String In Files
Dim t As New Thread(New ThreadStart(Sub()
Dim dispOp As DispatcherOperation = downloadProgress.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, New Action(Sub()
strFileName = s
Dim fileName As String = System.IO.Path.GetFileName(strFileName)
Dim destFile As String = System.IO.Path.Combine(TgtPath, fileName)
System.IO.File.Copy(strFileName, destFile, True)
lstTgrFiles.Items.Add(destFile)
downloadProgress.Value = lstTgrFiles.Items.Count
txtFileCopied.Text = lstTgrFiles.Items.Count & " File(s) Copied."
Thread.Sleep(100)
End Sub))
AddHandler dispOp.Completed, AddressOf dispOp_Completed
End Sub))
t.Start()
Next s
End Sub
The above code also shows an excellent feature of the WPF Threading mechanism. In WPF, we have been provided with the Dispatcher mechanism. This allows us to update WPF UI elements on the thread, which does not own the control to it. This facility was not provided in .NET 2.0. In the above code, I am using Dispatcher thread for ProgressBar control and updating ListBox control by adding files in it. In the TextBlock ‘txtFileCopied’, I am displaying the number of files being copied. The reason I am doing this is to provide user experience to the end user in a way that when files are getting copied in the target listbox, the ProgressBar will show the progress and the TextBlock ‘txtFileCopied’ will show files copied.
Write the following code in the Completed event of the ‘Dispatcher’:
C#
void dispOp_Completed(object sender, EventArgs e){
txtStatus.Visibility = System.Windows.Visibility.Collapsed;
btnCopy.IsEnabled = false;
}
VB.NET
Private Sub dispOp_Completed(ByVal sender As Object, ByVal e As EventArgs)
txtStatus.Visibility = System.Windows.Visibility.Collapsed
btnCopy.IsEnabled = False
End Sub
Step 4: Run the application.
Click on ‘Source Path’ button and the following result will be displayed:
Click on the copy files button and the following result will be displayed:
The entire source code of this article can be downloaded over here
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Mahesh Sabnis is a DotNetCurry author and a Microsoft MVP having over two decades of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions), and Front-end technologies like Angular and React. Follow him on twitter @
maheshdotnet or connect with him on
LinkedIn