BackgroundWorker报告另一个类的进度
我有一个类似于此链接中最后一个代码的代码:
http: //msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
但是 ComputeFibonacci 方法位于另一个类中,所以我的 doWork 方法将是这样的:
private void backgroundWorker1_DoWork(object sender,
DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
e.Result = new MyClass().ComputeFibonacci((int)e.Argument, worker, e);
}
我的代码锁定应用程序当我使用worker.ReportProgress(percentComplete);时永远如此 在另一个类中的 fibonaci 方法内部。 我认为问题是 backgroundWorker1_ProgressChanged 在另一个类中,而不是 MyClass 中。
请问我该怎么办?
如果我将斐波那契方法放在同一个类中,则不会出现该问题。但就我而言,将代码放在同一个类中是没有意义的。
谢谢
I have a code similar to the last code in this link:
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
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);
}
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.
What should I do please?
If I put the fibonaci method inside the same class, the problem won't occur. But in my case, doesn't make sence to put the code inside the same class.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让
MyClass
触发一个事件:类似这样的事情。
Make
MyClass
fire an event:Something like that.
ProgressChanged 事件处理程序应与 DoWork 事件处理程序位于同一类中。
在ComputeFibonacci 方法中,您将传入BackgroundWorker 对象,并调用worker.ReportProgress 方法。这应该调用 ProgressChanged 委托。
The ProgressChanged event handler should be in the same class as the DoWork event handler.
In your ComputeFibonacci method, you would pass in the BackgroundWorker object, and call the worker.ReportProgress method. This should call the ProgressChanged delegate.