Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

I have a windows form which referring an external DLL. I am accessing one of the public method which exposes by the DLL. Particular method is a big progress and its status should be indicated in the progress bar. DLL also have a public static variable which gives the progress status. my Concern is that it's not showing in the UI. only the the final value (100%) is showing.

Sample code is given below
Please help to identify where it goes wrong.

DLL code
C#
       public static void ProgressBarTest()
        {
            for (int i = 0; i < 100; i++)
            {
                // Wait 100 milliseconds.
                Thread.Sleep(100);
                // Report progress.
                TestSettings.ProgressValue = i;
            }
        }
public class TestSettings
    {
   public static int ProgressValue { get; set; }
     }


What I have tried:

C#
private void button1_Click(object sender, EventArgs e)
        {
            // Start the BackgroundWorker.
            //  backgroundWorker1.RunWorkerAsync();
            timer1.Enabled = true;          
            RetirementPlan.MainMethods.ProgressBarTest(); 
}


using Timer
C#
private void timer1_Tick(object sender, EventArgs e)
       {
           progressBar1.Minimum = 0;
           progressBar1.Maximum = 100;
           progressBar1.Value = 10;
           if (progressBar1.Value == progressBar1.Maximum)
           {
              // timer1.Stop();
               timer1.Enabled = false;
               progressBar1.Value = 0;
               progressBar1.Enabled = false;
           }
           progressBar1.Value = RetirementPlan.Utils.TestSettings.ProgressValue;
           double percentage = ((double)progressBar1.Value / (double)progressBar1.Maximum) * 100;
           lblProg.Text = "Current progress: " + percentage.ToString() + "%";
       }


BackGround Worker
C#
    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
      {
          // Change the value of the ProgressBar to the BackgroundWorker progress.
          progressBar1.Value = e.ProgressPercentage;
          // Set the text.
          this.Text = e.ProgressPercentage.ToString();
      }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
      {
          backgroundWorker1.ReportProgress(RetirementPlan.Utils.TestSettings.ProgressValue);
      }
Posted
Updated 2-Aug-16 8:49am
v3

1 solution

Your ProgressBarTest method never relinquishes control. The UI thread is blocked until the method returns, which is why you only ever see the final value, not the intermediate values.

If you want to use a BackgroundWorker, then you need to do the work in the DoWork event handler:
C#
private void button1_Click(object sender, EventArgs e)
{
    backgroundWorker1.RunWorkerAsync();
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    for (int i = 0; i < 100; i++)
    {
        Thread.Sleep(100);
        backgroundWorker1.ReportProgress(i);
    }
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
    lblProg.Text = "Current progress: " + e.ProgressPercentage + "%";
}


If you want to use a timer, then you need to do the work in a background thread, or in a manner that yields control to the UI thread. The simplest option would probably be an async method:
C#
public static async Task ProgressBarTestAsync()
{
    for (int i = 0; i < 100; i++)
    {
        await Task.Delay(100);
        TestSettings.ProgressValue = i;
    }
}
...
private async void button1_Click(object sender, EventArgs e)
{
    progressBar1.Minimum = 0;
    progressBar1.Maximum = 100;
    progressBar1.Value = 0;
    progressBar1.Enabled = true;
    timer1.Enabled = true;
    
    await RetirementPlan.MainMethods.ProgressBarTestAsync(); 
    
    timer1.Enabled = false;
    progressBar1.Value = 0;
    progressBar1.Enabled = false;
}

private void timer1_Tick(object sender, EventArgs e)
{
    progressBar1.Value = RetirementPlan.Utils.TestSettings.ProgressValue;
    double percentage = ((double)progressBar1.Value / (double)progressBar1.Maximum) * 100;
    lblProg.Text = "Current progress: " + percentage + "%";
}
 
Share this answer
 
v2
Comments
jinesh sam 3-Aug-16 5:10am    
Timer method works for me. Thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900