0

I'm having trouble incrementing my progress bar. I would like it to increment a certain value after every function ends. All the examples that I have looked at have functions that DO NOT take in any parameters. This is what I have so far

    public partial class TaskProgress: Form
        {
            public TaskProgress(string parameter, string parameter2, string parameter3)
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, System.EventArgs e)
            {
                // Start the BackgroundWorker.
               backgroundWorker1.WorkerReportsProgress = true;
                progressBar1.Value = 0;
                backgroundWorker1.RunWorkerAsync();
            }

            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                /*for (int i = 1; i <= 100; i++)
                {    Thread.Sleep(100);
                    backgroundWorker1.ReportProgress(i);}*/

                  function1(parameter1);
                  backgroundWorker1.ReportProgress(20);
                  function2(parameter2);
                  backgroundWorker1.ReportProgress(60);
                  function3(parameter3);
                  backgroundWorker1.ReportProgress(100);
            }

            private void backgroundWorker1_ProgressChanged(object sender,
                ProgressChangedEventArgs e)
            {
                // Change the value of the ProgressBar to the BackgroundWorker progress.
                progressBar1.Value = e.ProgressPercentage;
                // Set the text.
                this.Text = "Progress: " + e.ProgressPercentage.ToString() + "%";
            }
    }

How do I pass parameter values to function1, function2, and function3? The way Parameter1,2,3 is pass right now to function1,2,3 is wrong because those values are not defined in the backgroundworker function. Parameter1,2,3 are dynamic and passed on when the TaskProgress class is constructed.

Or is there another way of incrementing my progress bar based on the competition of my functions?

I have looked at this example, but the functions inside backgroundworker_dowork() do not need additional parameters How to use WinForms progress Bar?

I cannot call ReportProgress(); outside backgroundWorker1_DoWork because of the following code in my Form.cs[Design] code:

  // 
  this.backgroundWorker1.WorkerReportsProgress = true;
  this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
  this.backgroundWorker1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged);
  // 

I also cannot change the number of arguments backgroundWorker1_DoWork() take

Previously what i had was

      public partial class TaskProgress: Form
        {
            public TaskProgress(string parameter, string parameter2, string parameter3)
            {
                InitializeComponent();
                function1(parameter1);
                function2(parameter2);
                function3(parameter3);
            }

            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                for (int i = 1; i <= 100; i++)
                {    Thread.Sleep(100);
                    backgroundWorker1.ReportProgress(i);}
            }
       }

But the progress bar just moved on it's own; independent of when the functions finished

c newbie
  • 5
  • 2
  • Where are the `function1(parameter1)`, etc, defined in your question? You should show your complete code. – Enigmativity Feb 20 '19 at 23:47
  • 1
    "is there another way of incrementing my progress bar based on my functions?" Sure! You can call `backgroundWorker1.ReportProgress();` from within your functions...nothing wrong with that! – Idle_Mind Feb 20 '19 at 23:50
  • 1
    "How do I pass parameter values to function1, function2, and function3?" - Your code already shows exactly how to pass parameter values to those functions. – Enigmativity Feb 20 '19 at 23:51
  • And what do you mean by "incrementing my progress bar based on my functions"? – Enigmativity Feb 20 '19 at 23:51
  • Thanks for all the help, I have edited my question to be more specific and include more information – c newbie Feb 21 '19 at 00:17

1 Answers1

0

It seems to me that your simplest approach is to do this:

public partial class TaskProgress : Form
{
    private string parameter = null;
    private string parameter2 = null;
    private string parameter3 = null;

    public TaskProgress(string parameter, string parameter2, string parameter3)
    {
        InitializeComponent();
        this.parameter = parameter;
        this.parameter2 = parameter2;
        this.parameter3 = parameter3;
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        function1(this.parameter1);
        backgroundWorker1.ReportProgress(20);
        function2(this.parameter2);
        backgroundWorker1.ReportProgress(60);
        function3(this.parameter3);
        backgroundWorker1.ReportProgress(100);
    }
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172