Parallel.ForEach x of x

时间:2011-02-15 11:22:23

标签: c# wpf parallel-processing

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

exporting 0 of 25
exporting 0 of 25
...
exporting 5 of 25
exporting 5 of 25

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

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)
    });
});

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

由于

1 个答案:

答案 0 :(得分: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)
    });
});

编辑:我在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);
            });
        });
    }
}

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