Solved

How to link progress change event to progress bar

Posted on 2011-09-26
8
386 Views
Last Modified: 2012-05-12
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?
0
Comment
Question by:dpatel_team
  • 4
  • 3
8 Comments
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36600316
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
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36600363
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.
0
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 500 total points
ID: 36600375
This must solve the problem:
bg.ReportProgress(Convert.ToInt32((allreadtchecked / (float)size) * 100));

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36600785
Also, line #34, your "finished" message, should be in the RunWorkerCompleted() event of the BackgroundWorker().
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36600803
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?).
0
 

Author Comment

by:dpatel_team
ID: 36709557
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;
        }
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36709594
Hello, in your backgroundWorker1 properties window, be sure that ProgressChanged event is set to backgroundWorker1_ProgressChanged
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36709612
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

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now