c# - 在两个不同的类之间使用后台 worker 的进度条

标签 c# namespaces backgroundworker

  • 我在视觉工作室(2010 版)工作。
  • 我正在尝试基于另一个命名空间和类中的变量以一种形式(不同的命名空间和类)设置进度条。
  • 您在代码中看到的 ProgressPerc 变量来自另一个类(我已经使用“OtherNameSpace”指出了这一点。
  • 它告诉我我无法将 ProgressPerc 转换为 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的实例当您启动它时到后台工作人员,然后使用 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如图所示的处理程序只会执行一次。我认为您只是为了示例而将其简化了。

    关于c# - 在两个不同的类之间使用后台 worker 的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45330669/

    相关文章:

    c++ - 名称相同,来源不同的函数

    c++ - 命名空间与函数同名

    c# - BackgroundWorker 奇怪的问题

    c# - 如何屏蔽特定请求的敏感数据 (NLog)

    c# - 如果为 Web 应用程序选择 Linux + Mono + C# + MySQL,我将面临哪些障碍和限制?

    c# - 需要找到解决 C# 和 silverlight 的方法

    r - 尝试使用 R 包 magick 时 loadNamespace 出错

    c# - 让后台 worker 保持活力

    .net - 实现自定义 BackgroundWorker

    c# - 使图像可滚动