繁体   English   中英

在两个不同类之间使用后台工作程序的进度栏

[英]Progress Bar using Background worker between two different classes

  1. 我在Visual Studio(2010版)中工作。
  2. 我试图基于另一名称空间和类中的变量以一种形式(不同的名称空间和类)设置进度条。
  3. 您在代码中看到的ProgressPerc变量来自另一个类(我已经使用'OtherNameSpace'进行了指示。
  4. 它告诉我我无法将ProgressPerc转换为int(因为我无法将类型转换为int int)。

这里最最佳的解决方案是什么? 我想使用此变量来指示仿真的进度。

编辑:添加了ALMBerekeningen代码。 这只是其中的一小部分,完整的代码太多了,无法在此处显示。

谢谢!


public class ALMBerekeningen
{
    public int sim;
    public int Progress;
    public double ProgressPerc;

    this.ProgressPerc = this.sim / 1000;
    this.Progress = (int)Math.Round(this.Progress * 100f, 0, MidpointRounding.AwayFromZero);
}

Public class Form1: Form
{
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {

        ALMBerekeningen ProgressPerc;

        int sims;

        sims = (int)ProgressPerc;

        try
        {
            backgroundWorker1.ReportProgress(sims);
        }

        catch (Exception ex)
        {
            backgroundWorker1.CancelAsync();
            MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
        lblProgress.Text = "Completed " + progressBar1.Value.ToString() + " %";
        progressBar1.Update();
    }
}

您需要在启动时将ALMBerekeningen实例ALMBerekeningen给后台工作程序,然后使用事件处理程序中的DoWorkEventArgs.Argument属性对其进行访问:

public void Main()
{
     //The instance of the class with the variable for your progress bar
     ALMBerekeningen almBerekeningen = new ALMBerekeningen();

     BackgroundWorker bgw = new BackgroundWorker();
     bgw.DoWork += bgw_DoWork;

     //Pass your class instance in here
     bgw.RunWorkerAsync(almBerekeningen);
}


private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{

    //e.Argument is the instance of the class you passed in
    var progressPerc = (ALMBerekeningen)e.Argument;

    int sims;

    sims = progressPerc.ProgressPerc;

    try
    {
        backgroundWorker1.ReportProgress(sims);
    }

    catch (Exception ex)
    {
        backgroundWorker1.CancelAsync();
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

附带地,您显示的DoWork处理程序将只执行一次。 我认为您只是为了示例而将其简化了下来。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM