Solved

How to link progress change event to progress bar

Posted on 2011-09-26
8
390 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Keyboard 2 41
Problem!!! 8 37
tableview is not updating 1 22
Please explain purpose of GZIP 4 37
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

820 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