Parallel.ForEach x of x

Man*_*rin 3 c# parallel-processing wpf

所以我在c#4.0 WPF应用程序中工作并使用并行foreach循环使用我创建的数据库存储库将数据导出到数据库.我已经使用进度条使用并行foreach进行导出,但是希望能够提供更多深入的进度细节,例如导出第25项的第5项.我遇到的问题很明显,因为它正在运行与此同时,计数器不起作用,即总数会说出类似的结果

exporting 0 of 25
exporting 0 of 25
...
exporting 5 of 25
exporting 5 of 25
Run Code Online (Sandbox Code Playgroud)

任何人都可以指导如何在这样的并行循环中使行为工作:

int runningTotal = 0;
Parallel.ForEach(source, x =>
{
    Repository.Commit(x);
    runningTotal++;
    progressReporter.ReportProgress(() =>
    {
        //Progress bar update
        this.progressFile.Value++;
        this.lblProgress.Text = String
            .Format("Exporting Source {0} of {1}", runningTotal, source.Count)
    });
});
Run Code Online (Sandbox Code Playgroud)

希望这表明我希望实现的目标.

谢谢

Nek*_*esh 9

当您使用来自多个线程的相同数据时,您应该使用锁定机制保护其访问权限.通过使用该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)
    });
});
Run Code Online (Sandbox Code Playgroud)

编辑:我在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>
Run Code Online (Sandbox Code Playgroud)

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);
            });
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

我只是使用绑定来显示一个表明工作进度的字符串.所有内容都存储在Window中,该窗口实现INotifyPropertyChanged强制绑定以刷新并显示更新的值.它可以很容易地移动到它自己的类,它将实现INotifyPropertyChanged更清晰的代码.