Link to home
Start Free TrialLog in
Avatar of dpatel_team
dpatel_teamFlag for United States of America

asked on

How to link progress change event to progress bar

I have windows form application that run a a class method by BackgroundWorker.

I would like to add the windows form a progress bar to show the progress.

in the class method I have a foreach loop so I would like each loop to send the form event

with the current percentage.

this is what i do :
public partial class Form1 : Form
    {
        Parsser inst;

        public Form1()
        {
            InitializeComponent();
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.WorkerSupportsCancellation = true;
            inst = new Parsser( backgroundWorker1);
           
        }


        private void button2_Click(object sender, EventArgs e)
        {
            if (this.textBox1 != null & this.textBox2 != null)
            {
                if (backgroundWorker1.IsBusy != true)
                {
                    // Start the asynchronous operation.
                    backgroundWorker1.RunWorkerAsync();
                }
            }
        }



        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            inst.init(this.textBox1.Text, this.textBox2.Text);

            inst.ParseTheFile();
            System.Windows.Forms.MessageBox.Show("Parsing finish successfully");

        }
        private void backgroundWorker1_ProgressChanged(object sender,                ProgressChangedEventArgs e)
        {
            this.progressBar1.Value = e.ProgressPercentage;
        }

    }


}

Open in new window


and in the class i do this-
public Parser(BackgroundWorker bg)
    {
        bgReports = bg;
        
    }

    public void ParseTheFile()
    {
        Lines = File.ReadAllLines(FilePath);
        this.size = Lines.Length;
        foreach (string line in Lines)
        {

            bg.ReportProgress(allreadtchecked/size);

Open in new window


from some reason it dont work any idea?
Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

Hello, your code must work, just an observation, in your Parser class initialization you set bgReports = bg; but in your ParseTheFile method you call bg.ReportProgress instead of bgReports.ReportProgress, also, be sure that your reported progress is less or equal to your progressbar1.Maximum property
Oh that is the problem:
bg.ReportProgress(allreadtchecked/size);

Open in new window


it must be by default of your progress bar between 0 to 100, and allreadtchecked/size sure is returning always 0 because are int.
ASKER CERTIFIED SOLUTION
Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Tomlinson
Also, line #34, your "finished" message, should be in the RunWorkerCompleted() event of the BackgroundWorker().
I also wouldn't report progress for every single line as it will tax your main UI thread constantly updating.  Consider reporting only every xxx lines read, or use a Timer and update the progress every xxx milliseconds (maybe 500?).
Avatar of dpatel_team

ASKER

I am sorry but the issue is that the program never get to the

private void backgroundWorker1_ProgressChanged(object sender,  ProgressChangedEventArgs e)
        {
            this.progressBar1.Value = e.ProgressPercentage;
        }
Hello, in your backgroundWorker1 properties window, be sure that ProgressChanged event is set to backgroundWorker1_ProgressChanged
Did you wire up that event via the IDE?

You can also wire it up in code:
public Form1()
        {
            InitializeComponent();
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.WorkerSupportsCancellation = true;
            inst = new Parsser( backgroundWorker1);
   
            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged); // <--- wire up the ProgressChanged() event! 
        }

Open in new window