设计用于BackgroundWorker的接口 [英] Designing an Interface for BackgroundWorker

查看:132
本文介绍了设计用于BackgroundWorker的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Windows窗体应用程序我有一个扩展一个BackgroundWorker类,让我们ExtendedBGW1.cs把它在我的表单类我宣布它像一个成员变量,所以我有范围为整个类,像这样:

 公共部分类主营:表
{
    ExtendedBGW1 ebgw1;
}
 

后来的形式构造我这样做

 公开的Main()
{
    的InitializeComponent();

    ebgw1 =新ExtendedBGW1();

    InitializeBackgoundWorker();
}
 

我InitializeBackgroundWoker()方法看起来像这样

 私人无效InitializeBackgoundWorker()
{
    ebgw1.DoWork + =新DoWorkEventHandler(ebgw1.worker_DoWork);
    ebgw1.RunWorkerCompleted + =新RunWorkerCompletedEventHandler(processWorkerCompleted);
    ebgw1.ProgressChanged + =新ProgressChangedEventHandler(processProgressChanged);
    ebgw1.WorkerReportsProgress = TRUE;
    ebgw1.WorkerSupportsCancellation = TRUE;
}
 

现在来我的设计问题。我现在知道,我将有更多的不同的类,比如我extenededBGW1.cs,将延长BackgroundWorker的,所以我在想,如果我创建一个IExtenedBackGroundWorker我可以做这样的事情。

 公共部分类主营:表
{
    IExtenedBackGroundWorker ebgw1;
}
 

,仍然有合适的范围的主类。然后,我可以只创建取其IExtendedBackGroundWorker的实施,我需要稍后。

我可以做的没太大的问题的方法和属性的接口,但我真的遇到了一个问题,当我尝试正确连接起来的事件接口的基类,主类之间。

有没有人有什么想法?

下面是错误,我得到了主要

 错误1无法分配到DoWork的,因为它是一个方法组
 

和这里是我在我的界面实现误差

 错误5事件'System.ComponentModel.BackgroundWorker.DoWork只能出现在+ =或左边 -  =
 

下面是我的界面看起来像现在:

 接口IExtendedBackGroundWorker
{
    布尔IsBusy {获得; }

    布尔WorkerReportsProgress {获得;组; }

    布尔WorkerSupportsCancellation {获得;组; }

    名单< CompareObject> ObjList {获得;组; }

    字符串的文件路径{获得;组; }

    无效的RunWorkerAsync();

    无效CancelAsync();

    无效的DoWork();

    无效worker_DoWork(对象发件人,DoWorkEventArgs E);

    无效RunWorkerCompleted(对象发件人,RunWorkerCompletedEventArgs E);

    无效ProgressChanged(对象发件人,ProgressChangedEventArgs E);
}
 

解决方案

很简单,只需1 - 2 - 3 - 完成

 公共接口IMyWorker
{
    布尔WorkerReportsProgress {获得;组; }
    布尔WorkerSupportsCancellation {获得;组; }
    事件DoWorkEventHandler DoWork的;
    事件ProgressChangedEventHandler ProgressChanged;
    事件RunWorkerCompletedEventHandler RunWorkerCompleted;
}

公共类MyWorker:BackgroundWorker的,IMyWorker
{

}
 

用法:

 命名空间计算器
{
类节目
{
    静态无效的主要(字串[] args)
    {
        IMyWorker工人=新MyWorker();
        worker.DoWork + =新System.ComponentModel.DoWorkEventHandler(worker_DoWork);
    }

    静态无效worker_DoWork(对象发件人,System.ComponentModel.DoWorkEventArgs E)
    {
        抛出新的NotImplementedException();
    }
  }
}
 

玩得开心:)

In my windows forms applications I have a class that extends a backgroundworker, let's call it ExtendedBGW1.cs in my form class I declare it like a member variable so I have scope for the entire class like so:

public partial class Main : Form
{
    ExtendedBGW1 ebgw1;
}

Later on in the forms constructor I do this

public Main()
{
    InitializeComponent();

    ebgw1 = new ExtendedBGW1();

    InitializeBackgoundWorker();
}

My InitializeBackgroundWoker() method looks like this

private void InitializeBackgoundWorker()
{
    ebgw1.DoWork += new DoWorkEventHandler(ebgw1.worker_DoWork);
    ebgw1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(processWorkerCompleted);
    ebgw1.ProgressChanged += new ProgressChangedEventHandler(processProgressChanged);
    ebgw1.WorkerReportsProgress = true;
    ebgw1.WorkerSupportsCancellation = true;
}

Now comes my design problem. I know now that I am going to have different more classes like my extenededBGW1.cs that will extend BackGroundWorker so I was thinking that if I create a IExtenedBackGroundWorker I could do something like this.

public partial class Main : Form
{
    IExtenedBackGroundWorker ebgw1;
}

And still have the proper scope for the Main class. Then I could just create whichever implementation of IExtendedBackGroundWorker I need later on.

I can make the interface for the methods and properties without much issue but am really running into a problem when I try to wire up the events correctly between the interface the base class and the Main class.

Does anyone have any ideas?

Here are the errors I get in the Main

Error   1   Cannot assign to 'DoWork' because it is a 'method group'

and here is the error I get in my implemetation of the interface

Error   5   The event 'System.ComponentModel.BackgroundWorker.DoWork' can only appear on the left hand side of += or -= 

here is what my interface looks like right now:

interface IExtendedBackGroundWorker 
{
    bool IsBusy { get; }

    bool WorkerReportsProgress { get; set; }

    bool WorkerSupportsCancellation { get; set; }

    List<CompareObject> ObjList { get; set; }

    string FilePath { get; set; }

    void RunWorkerAsync();

    void CancelAsync();

    void DoWork();

    void worker_DoWork(object sender, DoWorkEventArgs e);

    void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e);

    void ProgressChanged(object sender, ProgressChangedEventArgs e);
}

解决方案

Easy, just 1 - 2 - 3 - Done

public interface IMyWorker
{
    bool WorkerReportsProgress { get; set; }
    bool WorkerSupportsCancellation { get; set; }
    event DoWorkEventHandler DoWork;
    event ProgressChangedEventHandler ProgressChanged;
    event RunWorkerCompletedEventHandler RunWorkerCompleted;
}

public class MyWorker : BackgroundWorker, IMyWorker
{

}

Usage:

namespace stackOverflow
{
class Program
{
    static void Main(string[] args)
    {
        IMyWorker worker = new MyWorker();
        worker.DoWork += new System.ComponentModel.DoWorkEventHandler(worker_DoWork);
    }

    static void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    {
        throw new NotImplementedException();
    }
  }
}

Have fun :)

这篇关于设计用于BackgroundWorker的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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