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

PIK*_*IKP 14 c# backgroundworker

在我的.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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

并在我的项目中名为"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;
        }
    } 
Run Code Online (Sandbox Code Playgroud)

但我不允许从"testClass"访问BackgroundWorker.

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

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

sa_*_*213 30

您可以将其作为变量传递

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;
    }
} 
Run Code Online (Sandbox Code Playgroud)

我认为最好的办法将是一个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;
    }
} 
Run Code Online (Sandbox Code Playgroud)

  • @Nolonar,那么,第二个版本从逻辑类分开`BackgroundWorker`,不仅如此类现在可以不管它被用`BacgroundWorker`或不使用返回的进展,你只需要订阅"OnProgressUpdate"事件就是这样. (3认同)