BackgroundWorker的reportProgress另一个类 [英] BackgroundWorker reportProgress in another class
问题描述
我有一个code相似,在这个环节的最后code:
I have a code similar to the last code in this link:
的http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
但ComputeFibonacci方法是另外一个类的内部,所以我的doWork的方法是这样的:
But the ComputeFibonacci method is inside another class, so my doWork method would be this:
private void backgroundWorker1_DoWork(object sender,
DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
e.Result = new MyClass().ComputeFibonacci((int)e.Argument, worker, e);
}
我的code锁定的应用程序,直到永远,当我使用worker.ReportProgress(percentComplete); 里面的Fibonaci方法,它是另一个类。我觉得问题是,backgroundWorker1_ProgressChanged是不是MyClass的另一个类中。
My code locks the application for ever when I use the worker.ReportProgress(percentComplete); inside the fibonaci method which is in another class. I think the problem is that the backgroundWorker1_ProgressChanged is inside another class, instead of MyClass.
我该怎么办吗?
如果我把Fibonaci数方法相同的类里面,会不会出现问题。但是,在我的情况下,不会使SENCE把code相同的类中。
感谢
推荐答案
请 MyClass的
触发事件:
public class MyClass
{
public event ProgressChangedEventHandler ProgressChanged;
protected virtual void OnProgressChanged(int progress)
{
if (ProgressChanged!= null)
{
ProgressChanged(this, new ProgressChangedEventArgs(progress, null));
}
}
public int ComputeFibonacci(int input)
{
//<Calculate stuff>
OnProgressChanged(currentProgress);
//...
return output;
}
}
private void backgroundWorker1_DoWork(object sender,
DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
var myClass = new MyClass();
myClass.ProgressChanged += (s, pe) => worker.ReportProgress(pe.ProgressPercentage);
myClass.ComputeFibonacci((int)e.Argument);
}
类似的东西。
Something like that.
这篇关于BackgroundWorker的reportProgress另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!