进度条值未从参考解决方案更新 [英] Progress bar value not get updated from reference solution

查看:56
本文介绍了进度条值未从参考解决方案更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我有一个引用外部DLL的Windows窗体。我正在访问DLL公开的公共方法之一。特殊方法是一个很大的进步,其状态应在进度条中指出。 DLL还有一个公共静态变量,它给出了进度状态。我担心的是它没有在UI中显示。只显示最终值(100%)。



示例代码如下:



DLL代码

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

       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:

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





使用计时器



using Timer

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



BackGround Worker

    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);
      }

推荐答案

你的 ProgressBarTest 方法永不放弃控制。 UI线程被阻塞,直到方法返回,这就是为什么你只看到最终值,而不是中间值。



如果你想使用 BackgroundWorker ,然后你需要在 DoWork 事件处理程序中完成工作:

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:
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 + "%";
}





如果你想使用计时器,那么你需要在后台线程中工作,或者在一种控制UI线程的方式。最简单的选项可能是 async 方法:



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:

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 + "%";
}


这篇关于进度条值未从参考解决方案更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆