BackgroundWorker的reportProgress另一个类 [英] BackgroundWorker reportProgress in another class

查看:96
本文介绍了BackgroundWorker的reportProgress另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆