【问题标题】:Designing an Interface for BackgroundWorker为 BackgroundWorker 设计一个接口
【发布时间】:2009-05-11 13:46:54
【问题描述】:

在我的 windows 窗体应用程序中,我有一个扩展后台工作程序的类,我们在我的窗体类中将其称为 ExtendedBGW1.cs 我将其声明为成员变量,因此我对整个类具有如下作用域:

public partial class Main : Form
{
    ExtendedBGW1 ebgw1;
}

稍后在表单构造函数中我会这样做

public Main()
{
    InitializeComponent();

    ebgw1 = new ExtendedBGW1();

    InitializeBackgoundWorker();
}

我的 InitializeBackgroundWoker() 方法如下所示

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

现在是我的设计问题。我现在知道我将拥有更多不同的类,比如我的扩展 BackGroundWorker 的扩展 BGW1.cs,所以我想如果我创建一个 IExtenedBackGroundWorker,我可以做这样的事情。

public partial class Main : Form
{
    IExtenedBackGroundWorker ebgw1;
}

并且仍然具有 Main 类的适当范围。然后我可以创建以后需要的任何 IExtendedBackGroundWorker 实现。

我可以毫无问题地为方法和属性创建接口,但是当我尝试在基类和主类的接口之间正确连接事件时,我真的遇到了问题。

有人有什么想法吗?

这是我在 Main 中遇到的错误

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

这是我在实现界面时遇到的错误

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

这是我的界面现在的样子:

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

【问题讨论】:

  • 你是如何定义接口的?
  • 我添加到我的 OP...对不起...我想我对如何处理事件处理程序感到困惑
  • IExtenedBackGroundWorker 是什么样子的,ExtendedBGW1 也是如此(由于该字段被声明为 ExtendedBGW1,因此常规 API 优先于 IExtenedBackGroundWorker)

标签: c# .net backgroundworker


【解决方案1】:

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

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
{

}

用法:

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

玩得开心:)

【讨论】:

    猜你喜欢
    • 2012-06-27
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多