I have two classes: Appleand Dog. Both of these classes have a method called at startup DoLotsOfWork()that publishes an event ProgressChanged.
public class Apple
{
public event EventHandler<ProgressChangedEventArgs> ProgressChanged;
private void OnProgressChanged(double progress)
{
if(ProgressChanged != null)
ProgressChanged(this, new ProgressChangedEventArgs((int)(progress * 100), null));
}
public void DoLotsOfWork()
{
for(lots of things)
{
...
OnProgressChanged(percentage);
}
}
}
To make the user interface lock, I run it with BackgroundWorker. I have Apple.ProgressChangedboth a Dog.ProgressChangedcall BackgroundWorker.ReportProgress(which triggers the event BackgroundWorker.ProgressChanged) to update the label and progress bar so that the user knows what is happening.
public class MainForm : Form
{
private Apple _apple;
private Dog _dog;
private bool _isAppleCompleted;
...
private void MainForm_Load(object sender, EventArgs e)
{
_apple.ProgressChanged += (a, args) => backgroundWorker1.ReportProgress(args.ProgressPercentage);
_dog.ProgressChanged += (a, args) => backgroundWorker1.ReportProgress(args.ProgressPercentage);
backgroundWorker1.RunWorkerAsync();
}
private void SetStatus(string status)
{
lblStatus.Invoke((Action)(() => lblStatus.Text = status));
}
private void SetProgress(int progress)
{
progressBar.Invoke((Action)(() => progressBar.Value = progress));
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
_isAppleCompleted = false;
SetStatus("Apple (Step 1/2)");
_apple.DoLotsOfWork();
_isAppleCompleted = true;
SetStatus("Dog (Step 2/2)");
_dog.DoLotsOfWork();
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
int progress = (_isAppleCompleted ? 50 : 0) + e.ProgressPercentage/2;
SetProgress(progress);
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
}
What I expect: Text "Apple (step 1/2)" as the progress bar moves from 0% to 50%. Then the phrase “Dog (step 2/2)” is displayed when the progress bar moves from 50% to 100%.
: "Dog ( 2/2)". 0% 100%, 50% 100%.
, , ; , Control.Invoke() Action, , - , . - , , ?
, , 0 <= e.ProgressPercentage <= 100 progressBar.Maximum = 100.