BackgroundWorker报告另一个类的进度

发布于 2024-11-03 09:44:13 字数 857 浏览 5 评论 0原文

我有一个类似于此链接中最后一个代码的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

糖果控 2024-11-10 09:44:13

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

类似这样的事情。

Make MyClass fire an event:

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.

小草泠泠 2024-11-10 09:44:13

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文