Please have a look at this example:
MainWindow.xaml
<Window x:Class="WpfApplication3.MainWindow"
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"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ProgressBar Name="ProgressBar" Height="20"></ProgressBar>
</Grid>
</Window>
MainWindow.xaml.cs
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace WpfApplication3
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
ProgressBar.Minimum = 0;
ProgressBar.Maximum = 100;
ProgressBar.Value = 0;
DoIt();
}
public void DoIt()
{
var task = new Task(() =>
{
for (var i = 0; i <= 100; i++)
{
SetProgressBarValue(i);
Thread.Sleep(100);
}
});
task.Start();
}
public void SetProgressBarValue(int value)
{
Application.Current.Dispatcher.Invoke(() =>
{
ProgressBar.Value = value;
});
}
}
}