报告从不同的类C#进度的BackgroundWorker [英] report progress backgroundworker from different class c#
问题描述
在我的.NET的C#项目我已经使用了BackgroundWorker的来调用不同的类中的方法。以下是我的主要形式的源 - code
In my .net c# project i have used a "backgroundworker" to call a method in a different class. 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();
}
}
和有在我的项目命名为testClass.cs一个单独的类文件中的以下code。我想报告的进展从这个类BackgroundWorker的,这样我就能够显示来自lable1在主要进度。
and have the following code in a separate class file named "testClass.cs" in my project. 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 lable1.
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。所以有人可以请告诉如何克服这个问题?
but I am not allowed to access backgroundworker from the "testClass". so can someone please tell how to overcome this problem?
p.s-我已经找到<一个href=\"http://stackoverflow.com/questions/3289920/c-sharp-how-to-report-progress-from-within-a-class-to-a-backgroundworker\">this解决方案,但我不明白。
p.s- I have found this solution, but i don't understand it.
推荐答案
您可以只通过它作为一个变量
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;
}
}
但是我认为最好的办法是一个事件
在的TestClass
的你的表格
可分配给
But I think the best option would be an event
in the testClass
that your Form
can assign to.
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;
}
}
这篇关于报告从不同的类C#进度的BackgroundWorker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!