Solved

How to link progress change event to progress bar

Posted on 2011-09-26
8
385 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

8 Experts available now in Live!

Get 1:1 Help Now