Update progress bar using value present in inner class

The two code segments below are located in different namespaces. And the second code access modifier is internal. I perform some operations there, and I want to calculate the percentage and update in the MgmntApp progress panel. How can i do this?

Wpfapplication1

MainWindow.xaml

<Window x:Class="WpfApplication1.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 Height="204">
        <ProgressBar Height="35" HorizontalAlignment="Left" Margin="57,83,0,0" Name="progressBar1" VerticalAlignment="Top" Width="346" />
    </Grid>
</Window>

I want to update the progress bar value by doing a lengthy operation in the class below.

Various

Fileparser.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Different
{
    /// <summary>
    /// </summary>
    internal class FileParser:ImageFileParser
    {
        ImageFileParser.GenerateCmds()
        {
            percentage=change;    //0 to 100
            //long time operation
        }
    }
}
+3
source share
2 answers

If parsing is time consuming, you can run it in a separate thread.

You can raise an event on FileParser when progress changes and subscribe to this event in MainWindow:

private void StartParsing()
{
    FileParser fp = new FileParser("FileName.txt");
    fp.ProgressChanged += FileParser_ProgressChanged;
    Thread t = new Thread(fp.GenerateCmds);
    t.Start();
}

private void FileParser_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    // switch to the UI thread if fileparser is running on a different thread
    Dispatcher.BeginInvoke(new Action(
                              () => { progressbar.Value = e.ProgressPercentage; }));
}

, FileParser:

internal class FileParser:ImageFileParser
{
    internal event EventHandler<ProgressChangedEventArgs> ProgressChanged;

    ImageFileParser.GenerateCmds()
    {
        percentage=change;    //0 to 100
        OnProgressChanged(percentage);
        //long time operation
    }

    internal protected void OnProgressChanged(int percentage)
    {
        var p = ProgressChanged;
        if(p != null)
        {
            p(this, new ProgressChangedEventArgs(percentage, null));
        }
    }
}
+2

FileParser?

<ProgressBar Value="{Binding MyFileParser.PercentComplete}" ...

FileParser INotifyPropertyChanged:

internal class FileParser:ImageFileParser, INotifyPropertyChanged
{
    private decimal _pct;
    internal decimal PercentComplete { 
        get { return _pct; }
        set {
            _pct = value;
            PropertyChanged(this, new PropertyChangedEventArgs("PercentComplete"));
        }
    }
    PropertyChanged(this, new PropertyChangedEventArgs(info));

    ImageFileParser.GenerateCmds()
    {
        PercentComplete = change;    //0 to 100
        //long time operation
    }
}

PercentComplete ...

+1

All Articles