Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to link progress change event to progress bar

Posted on 2011-09-26
8
Medium Priority
?
398 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 total points
ID: 36600375
This must solve the problem:
bg.ReportProgress(Convert.ToInt32((allreadtchecked / (float)size) * 100));

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36600785
Also, line #34, your "finished" message, should be in the RunWorkerCompleted() event of the BackgroundWorker().
0
 
LVL 86

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 86

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

618 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