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