16

在我的 .NET C# 项目中,我使用了“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;
        }
    } 

但我不允许从“testClass”访问 BackgroundWorker。

有人可以告诉如何克服这个问题吗?

ps-我找到了这个解决方案,但我不明白。

4

3 回答 3

33

您可以将其作为变量传递

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

我认为最好的选择是eventtestClass可以Form分配的。

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;
    }
} 
于 2013-02-14T09:39:27.073 回答
1

我刚刚遇到了同样的问题(我的长期运行过程是数据库还原),并通过在另一个类中引发一个事件以类似的方式解决了它,但随后让我对该事件的订阅者充当了 backgroundWorker1 的包装器.报告进度()。

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

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

于 2016-04-12T11:02:30.207 回答
-2

我在上面的代码中错过了这个吗?:

backgroundWorker1 = new BackgroundWorker();

backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);

于 2018-09-10T19:09:20.747 回答