T
tarajali
Guest
The Situation: I have seen one thread that asks a similar question but is more convoluted than what I am trying to accomplish. I am migrating from MATLAB to C# so I am a novice with event handlers. I am able to run one BackgroundWorker process at a time with each having its own button and progress bar.
The Problem: I would like to run three BackgroundWorker processes by using one button (i.e. by performing one click) and having the tasks completed in sequence i.e. backgroundWorker1 (and its progress bar and "stage 1 complete" label) followed by backgroundWorker2 (and its progress bar and "stage 2 complete" label) followed by backgroundWorker3 (and its progress bar and "stage 3 complete" label). Thank you.
The Code: Below is the whole code that I currently have but it only executes backgroundWorker1 (and its progress bar and "stage 1 complete" label) since I do not know how to incorporate backgroundWorker2 and backgroundWorker3. Form1.cs has 1 Button, 3 ProgressBar, 3 Label, and 3 BackgroundWorker components.
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
namespace BackGroundWorkerExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int progBar1Max = 25;
private int progBar2Max = 10;
private int progBar3Max = 50;
private void button1_Click(object sender, EventArgs e)
{
// start backgroundWorker1
backgroundWorker1.RunWorkerAsync();
// disable button after click
button1.Enabled = false;
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int progBar1Val = 1; progBar1Val <= progBar1Max; progBar1Val++)
{
// wait 100 milliseconds
Thread.Sleep(100);
// report progress
backgroundWorker1.ReportProgress(progBar1Val);
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// change the value of progressBar1 to the backgroundWorker1 progress
progressBar1.Value = e.ProgressPercentage;
progressBar1.Maximum = progBar1Max;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
label1.Text = "stage 1 complete";
}
private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
for (int progBar2Val = 1; progBar2Val <= progBar2Max; progBar2Val++)
{
// wait 250 milliseconds
Thread.Sleep(250);
// report progress
backgroundWorker2.ReportProgress(progBar2Val);
}
}
private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// change the value of the ProgressBar to the BackgroundWorker progress
progressBar2.Value = e.ProgressPercentage;
progressBar2.Maximum = progBar2Max;
// set the text
this.Text = e.ProgressPercentage.ToString();
}
private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
label2.Text = "stage 2 complete";
}
private void backgroundWorker3_DoWork(object sender, DoWorkEventArgs e)
{
for (int progBar3Val = 1; progBar3Val <= progBar3Max; progBar3Val++)
{
// wait 50 milliseconds
Thread.Sleep(50);
// report progress
backgroundWorker3.ReportProgress(progBar3Val);
}
}
private void backgroundWorker3_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// change the value of the ProgressBar to the BackgroundWorker progress
progressBar3.Value = e.ProgressPercentage;
progressBar3.Maximum = progBar3Max;
// set the text
this.Text = e.ProgressPercentage.ToString();
}
private void backgroundWorker3_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
label3.Text = "stage 3 complete";
}
}
}
Continue reading...
The Problem: I would like to run three BackgroundWorker processes by using one button (i.e. by performing one click) and having the tasks completed in sequence i.e. backgroundWorker1 (and its progress bar and "stage 1 complete" label) followed by backgroundWorker2 (and its progress bar and "stage 2 complete" label) followed by backgroundWorker3 (and its progress bar and "stage 3 complete" label). Thank you.
The Code: Below is the whole code that I currently have but it only executes backgroundWorker1 (and its progress bar and "stage 1 complete" label) since I do not know how to incorporate backgroundWorker2 and backgroundWorker3. Form1.cs has 1 Button, 3 ProgressBar, 3 Label, and 3 BackgroundWorker components.
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
namespace BackGroundWorkerExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int progBar1Max = 25;
private int progBar2Max = 10;
private int progBar3Max = 50;
private void button1_Click(object sender, EventArgs e)
{
// start backgroundWorker1
backgroundWorker1.RunWorkerAsync();
// disable button after click
button1.Enabled = false;
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int progBar1Val = 1; progBar1Val <= progBar1Max; progBar1Val++)
{
// wait 100 milliseconds
Thread.Sleep(100);
// report progress
backgroundWorker1.ReportProgress(progBar1Val);
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// change the value of progressBar1 to the backgroundWorker1 progress
progressBar1.Value = e.ProgressPercentage;
progressBar1.Maximum = progBar1Max;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
label1.Text = "stage 1 complete";
}
private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
for (int progBar2Val = 1; progBar2Val <= progBar2Max; progBar2Val++)
{
// wait 250 milliseconds
Thread.Sleep(250);
// report progress
backgroundWorker2.ReportProgress(progBar2Val);
}
}
private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// change the value of the ProgressBar to the BackgroundWorker progress
progressBar2.Value = e.ProgressPercentage;
progressBar2.Maximum = progBar2Max;
// set the text
this.Text = e.ProgressPercentage.ToString();
}
private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
label2.Text = "stage 2 complete";
}
private void backgroundWorker3_DoWork(object sender, DoWorkEventArgs e)
{
for (int progBar3Val = 1; progBar3Val <= progBar3Max; progBar3Val++)
{
// wait 50 milliseconds
Thread.Sleep(50);
// report progress
backgroundWorker3.ReportProgress(progBar3Val);
}
}
private void backgroundWorker3_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// change the value of the ProgressBar to the BackgroundWorker progress
progressBar3.Value = e.ProgressPercentage;
progressBar3.Maximum = progBar3Max;
// set the text
this.Text = e.ProgressPercentage.ToString();
}
private void backgroundWorker3_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
label3.Text = "stage 3 complete";
}
}
}
Continue reading...