C# 报告来自不同c类的后台工作人员的进度#

C# 报告来自不同c类的后台工作人员的进度#,c#,backgroundworker,C#,Backgroundworker,在我的.NETC#项目中,我使用了一个“BackgroundWorker”来调用另一个类中的方法。下面是我的主窗体的源代码 public partial class Form1 : Form { public Form1() { InitializeComponent(); } testClass t1 = new testClass(); private void backgroun

在我的.NETC#项目中,我使用了一个“BackgroundWorker”来调用另一个类中的方法。下面是我的主窗体的源代码

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        testClass t1 = new testClass();
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            t1.changevalue(1000);
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            label1.Text += Convert.ToString(e.ProgressPercentage);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }
    }
并在我的项目中的一个名为“testClass.cs”的单独类文件中包含以下代码。我想从这个类向BackgroundWorker报告进度,这样我就可以从label1在main中显示进度

class testClass
    {
        private int val;
        public int changevalue(int i)
        {
            for (int j = 0; j < 1000; j++)
            {
                val += i + j;
                //from here i need to preport the backgroundworker progress
                //eg; backgroundworker1.reportProgress(j);
            }
            return val;
        }
    } 
class测试类
{
私人国际旅行社;
公共整数转换值(整数i)
{
对于(int j=0;j<1000;j++)
{
val+=i+j;
//从这里我需要预告后台工作人员的进度
//背景工作1.报告进展(j);
}
返回val;
}
} 
但我不允许从“testClass”访问BackgroundWorker

有人能告诉我如何克服这个问题吗


p、 我找到了,但我不明白

您可以将其作为变量传入

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    t1.changevalue(1000, sender as BackgroundWorker);
}


class testClass
{
    private int val;
    public int changevalue(int i, BackgroundWorker bw)
    {
        for (int j = 0; j < 1000; j++)
        {
            val += i + j;
            bw.ReportProgress(i);
            //from here i need to preport the backgroundworker progress
            //eg; backgroundworker1.reportProgress(j);
        }
        return val;
    }
} 

我刚刚遇到了同样的问题(我的长时间运行的过程是数据库还原),并通过在另一个类中引发事件以类似的方式解决了这个问题,但我的事件订阅方只是充当backgroundWorker1.ReportProgress()的包装器

这节省了您必须使用的时间:

base.Invoke((Action)delegate

如果表单意外关闭,我认为这可能会导致问题?

我是否在上述代码中遗漏了这一点

backgroundWorker1=新的BackgroundWorker()


backgroundWorker1.DoWork+=新的DoWorkerVenthandler(backgroundWorker1_DoWork)

比你还厉害,萨乌·达姆213先生。这正是我要找的。你的代码很有魅力。干杯您的第二个解决方案看起来更难阅读。为什么你认为它比第一个好?@Nolonar,好吧,第二个版本将
BackgroundWorker
与逻辑类分开,不仅类现在可以返回进度,而不管它是否与
bagroundworker
一起使用,你只需要订阅“OnProgressUpdate”事件就可以了。我不确定这是否可以作为答案。你不觉得吗?
private void DBRestoreProgressHandler(DataAccess da, DataAccess.DatabaseRestoreEventArgs e)
    {
        backgroundWorker1.ReportProgress(e.RestoreProgress);
    }

private void backgroundWorker1_ReportProgress(object sender, ProgressChangedEventArgs e)
    {
        someLabel.Text = e.ProgressPercentage.ToString();
    }
base.Invoke((Action)delegate