Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 952493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:54:08+00:00 2026-05-15T23:54:08+00:00

I’m trying to make a progress bar show up on a form, but for

  • 0

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
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:54:09+00:00Added an answer on May 15, 2026 at 11:54 pm

    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:

    • Create the instance of the BackgroundWorker
    • Set the WorkerSupportsProgress property to true (the default is false)
    • Subscribe to the DoWork event
    • In the DoWork event handler, make calls to the ReportProgress method to report your progress
    • Subscribe to the ProgressChanged event, this is where you will update your progress bar. This is also the event htat is fired when you call the ReportProgrss method mentioned before
    • Subscribe to the RunWorkerCompleted event, this is where you will close your form when done
    • Call the RunWorkerAsync method to start the asynchronous operation

    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:

    public void SpawnPizzaProgressBarForm(object sender, EventArgs e)
    {
        FormPizzaProgressBar Form = new FormPizzaProgressBar();
        Form.ShowDialog();
    }
    
    ...
    
    BackgroundWorker worker = new BackgroundWorker();
    
    public void ProgressBarForm_Load(object sender, EventArgs e)
    {
        // Initialize the background worker.
        worker = new BackgroundWorker();
    
        // Indicate that the worker supports progress.
        worker.WorkerSupportsProgress = true;
    
        // Subscribe to the DoWork event.
        worker.DoWork += (s, e) => {
            // Create the pizza instance.
            Pizza = new Pizza();
    
            // Process the slices.
            foreach (var Slice in Pizza)
            {
                // Clear the slice.
                Slice.Clear();
    
                // Report the progress.
                worker.ReportProgress(Slice.Index / Pizza.Count() * 100);
            }
        };
    
        // Subscribe to the ProgressChanged event.
        worker.ProgressChanged = (s, e) => {
            // Update the progress bar.
            PizzaEatingProgressBar.Value = e.ProgressPercentage;
        };
    
        // Subscribe to the RunWorkerCompleted event.
        worker.RunWorkerCompleted = (s, e) => {
            // Close the dislog.
            this.Close();
        };
    }
    
    // Must override to properly dispose of the background worker.
    protected override void Dispose(bool disposing)
    {
        // Call the base.
        base.Disposing(disposing);
    
        // Dispose of the background worker if disposing is true.
        if (disposing) worker.Dispose();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.