WPF : Progress Bar update in Parallel.Foreach
<p>I am trying to update the progress bar while running a <code>Parallel.Foreach</code>, but during execution nothing happens. Progressbar gets updated only when the For loop end. How can I make this code work?</p>

<p><strong>XAML</strong></p>

<pre><code> &lt;StackPanel&gt;
        &lt;Grid x:Name="LoadProgressGrid" Height="100"                  
                  Visibility="Visible"&gt;
            &lt;ProgressBar x:Name="LoadProgress"
                Maximum="100"
                Minimum="1" /&gt;
            &lt;TextBlock Margin="0,0,0,-5"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                FontSize="16"&gt;&lt;Run Text="Import Progress"/&gt;&lt;/TextBlock&gt;   
        &lt;/Grid&gt;
        &lt;Button  Height="35" Width="100" Margin="0,10" Content="Test" Click="Test_Click"/&gt;
    &lt;/StackPanel&gt;
</code></pre>

<p><strong>C#</strong> </p>

<pre><code> private void Test_Click(object sender, RoutedEventArgs e)
        {
              decimal current=0;  
              List&lt;string&gt; lst = new List&lt;string&gt;();

              lst.Add("Foo");
              lst.Add("Foo");
              lst.Add("Foo");
              lst.Add("Foo");

              decimal max = 100000;

              var uiFactory = new TaskFactory(TaskScheduler.FromCurrentSynchronizationContext());

              Parallel.ForEach(lst,  (data) =&gt;
              {
                for (int i = 0; i &lt; max; i++)
                {
                  // Use the uiFactory above:
                  // Note the need for a temporary here to avoid closure issues!
                  current = current + 1;
                  uiFactory.StartNew( () =&gt; LoadProgress.Value = (double)(current/max)*100);
                }
              });


              MessageBox.Show("Done!");
         }   
</code></pre>