Im trying to use backgroundWorker to show a progressBar before app loaded:

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chocolade
    New Member
    • Aug 2010
    • 69

    Im trying to use backgroundWorker to show a progressBar before app loaded:

    The splash screen is running for 3-5 seconds then close.

    Then after it i want to show a progressBar on the screen from the time the splash screen have been closed untill the application is loaded.

    This is the code in the constructor of Form1 of the splash screen and the backgroundWorke r:


    Code:
    while (splash_flag == true)
                {
                    splash.Show();
                    Thread.Sleep(3000);
                    splash_flag = false;
                }
                if (splash_flag == false)
                {
    
                    splash.Close();
                }
                myProgressBar2 = new ProgressBar();
                {
                    myProgressBar2.Maximum = 100;
                    backgroundWorker1.WorkerReportsProgress = true;
                    backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
                    backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
                    backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
                };
                backgroundWorker1.RunWorkerAsync(myProgressBar2);

    Now here is the functions of the backgroundWorke r on the bottom of Form1 code:

    Firs the backgroundWorke r1_DoWork:


    Code:
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
             {
                 for (int i = 0; i < 100; i++)
                 {
    
                     Thread.Sleep(100);
    
                     backgroundWorker1.ReportProgress(i); 
    
                 }
    
             }
    Then the other two functions:


    Code:
    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) //call back method
             {
    
                 myProgressBar2.Value = e.ProgressPercentage;
    
             }
             private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) //call back method
             {
    
                 myProgressBar2.Value = progressBar1.Maximum;
    
             }
    But nothing happen. When i run the application i see the splash screen thats ok after it nothing waiting for the application to load.

    I want that while the form1 making connection tests to the internet it will show me how long it takes to the application to load.

    This is the code of the backgroundWorke r with the internet connection tests this time.

    I want that after the splash screen when its making internet connection tests so it will show me in this time how much time left to load the application:


    Code:
    myProgressBar2 = new ProgressBar();
                {
                    myProgressBar2.Maximum = 100;
                    backgroundWorker1.WorkerReportsProgress = true;
                    backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
                    backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
                    backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
                };
                backgroundWorker1.RunWorkerAsync(myProgressBar2);
    
    
                if (fdt.http_test() == true)
                {
                    Client.DownloadFile(satellite_address, temp_dir + satellite_file_name);
                    label6.Enabled = true;
                    label6.Visible = true;
                    label6.Text = "internet connection is active";
                }
                else
                {
                    Logger.Write("Connection to the internet has failed");
                    button1.Enabled = true;
                    timer1.Enabled = false;
                    label6.Enabled = true;
                    label6.Visible = true;
                }
                if (fdt.satellite_test() == true)
                {
                    Client.DownloadFile(satellite_address, temp_dir + satellite_file_name);
                    label6.Enabled = true;
                    label6.Visible = true;
                    label6.Text = "internet connection is active";
                }
                else
                {
                    Logger.Write("Connection to the internet has failed");
                    button1.Enabled = true;
                    timer5.Enabled = false;
                    label6.Enabled = true;
                    label6.Visible = true;
                }
                if (fdt.http_test() == true)
                {
                    Client.DownloadFile(remote_image_on_server, temp_dir + temp_file);
                    label6.Enabled = true;
                    label6.Visible = true;
                    label6.Text = "internet connection is active";
                }
                else
                {
                    Logger.Write("Connection to the internet has failed");
                    button1.Enabled = true;
                    timer1.Enabled = false;
                    label6.Enabled = true;
                    label6.Visible = true;
                }

    Thanks.
  • cloud255
    Recognized Expert Contributor
    • Jun 2008
    • 427

    #2
    You have not assigned your progress bar to the collection of controls of the form object.

    I have added 2 lines of code (23 and 24). That should solve your problem.

    The progress bar control will not render until you give it a parent and call she show method. You could have used the designer to add the progress bar to the form, that would have generated the missing code for you in the Designer.cs file.

    You might also want to have a look at your general design before going live with this project.

    HTH

    Code:
       1. while (splash_flag == true)
       2.             {
       3.                 splash.Show();
       4.                 Thread.Sleep(3000);
       5.                 splash_flag = false;
       6.             }
       7.             if (splash_flag == false)
       8.             {
       9.  
      10.                 splash.Close();
      11.             }
      12.             myProgressBar2 = new ProgressBar();
      13.             {
      14.                 myProgressBar2.Maximum = 100;
      15.                 backgroundWorker1.WorkerReportsProgress = true;
      16.                 backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
      17.                 backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
      18.                 backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
      19.             };
      20.             backgroundWorker1.RunWorkerAsync(myProgressBar2);
      21.  
      22.
      23.             myProgressBar2.Parent = this;
      24.             myProgressBar2.Show();

    Comment

    • Chocolade
      New Member
      • Aug 2010
      • 69

      #3
      It didnt work so far.

      I tried what you said but so far i cant see the progressbar after the splash screen.



      This is what i have added to the constructor:
      Code:
      myProgressBar2 = new ProgressBar();
                  {
                      myProgressBar2.Maximum = 100;
                      myProgressBar2.Parent = this;
                      myProgressBar2.Show();
                      backgroundWorker1.WorkerReportsProgress = true;
                      backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
                      backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
                      backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
                      backgroundWorker1.RunWorkerAsync(myProgressBar2);
                  }
      I tried also to put this line after the Maximum=100;
      Code:
      this.Controls.Add(myProgressBar2);
      But this line didnt help either.

      I tried to remove the {} in the constructor as above didnt help too.

      Now this is the event of the backgroundworke r1:


      Code:
      private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
               {
                   for (int i = 0; i < 100; i++)
                   {
                       Thread.Sleep(100);
                       backgroundWorker1.ReportProgress(i);
                   }
               }
      And the other two functions under it:


      Code:
      private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) //call back method 
               {
                   myProgressBar2.Value = e.ProgressPercentage;
               }
               private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) //call back method 
               {
                   myProgressBar2.Value = progressBar1.Maximum;
      
               }

      Whats wrong why i dont see the progressbar in the middle of the screen after the splash screen?



      Thanks.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        If your form hasn't loaded, how can it be drawing the progress bar?

        Comment

        Working...