Поскольку вы используете одни и те же данные из нескольких потоков, вы должны защитить их доступ с помощью механизма блокировки.Это можно легко сделать с помощью конструкции lock
.
Однако, это может быть слишком излишним для ваших нужд.Поскольку вы только увеличиваете значение, будет работать простой System.Threading.Interlocked.Increment
. msdn .
int runningTotal = 0;
Parallel.ForEach(source, x =>
{
Repository.Commit(x);
Interlocked.Increment(ref runningTotal);
progressReporter.ReportProgress(() =>
{
//Progress bar update
Interlocked.Increment(ref this.progressFile.Value);
this.lblProgress.Text = String
.Format("Exporting Source {0} of {1}", runningTotal, source.Count)
});
});
РЕДАКТИРОВАТЬ: я сделал образец в WPF.
Window.xaml:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition />
</Grid.RowDefinitions>
<Button Grid.Row="0" Click="Button_Click">Click me</Button>
<TextBlock Grid.Row="1">
<TextBlock.Text>
<MultiBinding StringFormat="Progress: {0}/{1}">
<Binding Path="ProgressValue" RelativeSource="{RelativeSource AncestorType=Window, Mode=FindAncestor}" />
<Binding Path="ProgressMax" RelativeSource="{RelativeSource AncestorType=Window, Mode=FindAncestor}" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Grid>
</Window>
Window.xaml.cs
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
}
int progressValue;
public int ProgressValue
{
get { return (this.progressValue); }
set { this.progressValue = value; this.raisePropertyChanged("ProgressValue"); }
}
public int ProgressMax
{
get { return (100); }
}
public event PropertyChangedEventHandler PropertyChanged;
void raisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Task.Factory.StartNew(() =>
{
int counter = 0;
Parallel.ForEach(Enumerable.Range(1, 100), i =>
{
Interlocked.Increment(ref counter);
this.ProgressValue = counter;
Thread.Sleep(25);
});
});
}
}
Я просто использую привязки для отображения строки, показывающей ход работы.Все хранится в окне, которое реализует INotifyPropertyChanged
, чтобы принудительно обновлять привязки и отображать обновленное значение.Его можно легко переместить в собственный класс, который будет реализовывать INotifyPropertyChanged
для более чистого кода.