I’m trying to make a progress bar show up on a form, but for some reason the form isn’t actually visible until the process is over, and it is closed when the process is over (or in other words, the form is only open for an instant). How can I make it so the form shows up at the beginning of the process?
Note: my code might not be 100% correct, I’m just trying to make it different than my own for confidentiality reasons.
public void SpawnPizzaProgressBarForm(object sender, EventArgs e)
{
FormPizzaProgressBar Form = new FormPizzaProgressBar();
Form.ShowDialog();
}
...
public void ProgressBarForm_Load(object sender, EventArgs e)
{
Pizza = new Pizza();
Pizza.Eat(PizzaEatingProgressBar);
this.Close();
}
...
public void Eat(ProgressBar PizzaEatingProgressBar)
{
foreach(var Slice in Pizza)
{
Slice.Clear(); //
PizzaEatingProgressBar.Value = (Slice.Index / Pizza.Count())*100
}
}
This happens because you do all the processing in the Load event for the form. This is called before the form is shown for the first time.
While in the event handler, you are preventing the form from actually showing, as the event handler has to complete before anything else can be processed.
What you want to do is use a BackgroundWorker instance to perform your work. This requires you to do the following:
You have some issues here in that you have your Pizza class tightly coupled to the progress bar. This isn’t a good idea. Rather, you should have an event that is fired to indicate that the progress has changed, and then call the ProgressChanged event from the event handler for your Pizza instance.
I’ve moved out the code for your Eat method and encapsulated it in the form to show you an example of how to use the BackgroundWorker class, but the ideal solution would be to expose an event to indicate when the amount of pizza consumed changes.
Also note that you should override the protected Dispose method on the Form class to properly dispose of the BackgroundWorker instance when the form is disposed of.
Here is what the example looks like: