简体   繁体   English

报告来自不同类c#的进度backgroundworker

[英]report progress backgroundworker from different class c#

In my .NET C# project I have used a "BackgroundWorker" to call a method in a different class. 在我的.NET C#项目中,我使用“BackgroundWorker”来调用另一个类中的方法。 The following is the source-code of my main form 以下是我的主要表格的源代码

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

and have the following code in a separate class file named "testClass.cs" in my project. 并在我的项目中名为“testClass.cs”的单独类文件中包含以下代码。 I want to report the progress to the BackgroundWorker from this class, so that I will be able to display the progress in the main from label1. 我想从此类向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;
        }
    } 

but I am not allowed to access BackgroundWorker from the "testClass". 但我不允许从“testClass”访问BackgroundWorker。

Can someone please tell how to overcome this problem? 有人可以告诉我如何克服这个问题?

ps- I have found this solution , but I don't understand it. ps-我找到了这个解决方案 ,但我不明白。

You could just pass it in as a variable 您可以将其作为变量传递

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

But I think the best option would be an event in the testClass that your Form can assign to. 我认为最好的选择是您的Form可以分配给testClass中的一个event

public partial class Form1 : Form
{
    private BackgroundWorker backgroundWorker1;
    private testClass t1 = new testClass();

    public Form1()
    {
        InitializeComponent();

        // subscribe to your event
        t1.OnProgressUpdate += t1_OnProgressUpdate;
    }

    private void t1_OnProgressUpdate(int value)
    {
        // Its another thread so invoke back to UI thread
        base.Invoke((Action)delegate
        {
            label1.Text += Convert.ToString(value);
        });
    }

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

    private void button1_Click_1(object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();
    }

}

class testClass
{
    public delegate void ProgressUpdate(int value);
    public event ProgressUpdate OnProgressUpdate;

    private int val;
    public int changevalue(int i)
    {
        for (int j = 0; j < 1000; j++)
        {
            val += i + j;

            // Fire the event
            if (OnProgressUpdate != null)
            {
                OnProgressUpdate(i);
            }
        }
        return val;
    }
} 

I've just had this same issue (my long running process is a database restore), and solved it in a similar way by raising an event in the other class, but then had my subscriber to that event just act as a wrapper to backgroundWorker1.ReportProgress(). 我刚刚遇到同样的问题(我的长时间运行过程是数据库恢复),并通过在另一个类中引发事件以类似的方式解决它,但后来我的订阅者只是充当了backgroundWorker1的包装器.ReportProgress()。

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

This saved having to use: 这节省了必须使用:

base.Invoke((Action)delegate

Which I think can then cause problems if the form closes unexpectedly? 如果表单意外关闭,我认为可能会导致问题?

Am i missing this in the above code ?: 我在上面的代码中错过了这个吗?:

backgroundWorker1 = new BackgroundWorker(); backgroundWorker1 = new BackgroundWorker();

backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); backgroundWorker1.DoWork + =新的DoWorkEventHandler(backgroundWorker1_DoWork);

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

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