Parallel .ForEach x of x

So, I work in a WPF C # 4.0 application and use parallel foreach loops to export data to the database using the database repository I created. I have an export that works with parallel foreach using a progress bar, but I would like to get more detailed information about the progress, for example, export element 5 of 25. The problem that I encountered is obvious, since it works In parallel, the counter does not work, those. the amount will say something like

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

Can anyone give any information on how to make the behavior work in a parallel loop like this:

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

Hope this shows what I hope to achieve.

thank

+3
source share
1

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

EDIT: 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 .

+9

Source: https://habr.com/ru/post/1792206/


All Articles