【问题标题】:How to generate callback (event) from library to application in c#如何在 C# 中生成从库到应用程序的回调(事件)
【发布时间】:2013-05-05 20:20:56
【问题描述】:

我正在开发一个库 (DLL),我需要在其中向用户提供事件(中断)作为一种数据方法。图书馆的工作是开始在套接字上列出,从套接字接收数据并以一种方法将这些数据传递给用户。

图书馆:

public void start(string IP, int port)
{
    // start logic...

    // receives data from socket... send this data to user

}

应用:

Library. Class a = new Library. Class();
a.start(ip, port);

// I need this method called by library automatically when it receives data...

void receivedData(string data)
{
    // data which received by library....
}

如何使用库中的数据向应用程序发起事件?

提前谢谢....

【问题讨论】:

    标签: c# oop events dll


    【解决方案1】:

    您应该更改 start 方法的签名以传递委托:

    public void start(string IP, int port, Action<string> callback)
    {
        // start logic...
    
        // receives data from socket... send this data to user
        callback(data);
    }
    
    Library. Class a = new Library. Class();
    a.start(ip, port, receivedData);
    
    // I need this method called by library automatically when it receives data...
    
    void receivedData(string data)
    {
        // data which received by library....
    }
    

    【讨论】:

      【解决方案2】:

      像这样将事件添加到您的库中:

      public event Action<string> OnDataReceived = null;
      

      然后,在应用程序中:

      Library.Class a = new Library.Class();
      a.OnDataReceived += receivedData;
      a.start(ip, port);
      

      就是这样。

      但是你可能想用约定来编写事件,我建议你开始习惯它,因为 .NET 就是这样使用事件的,所以每当你遇到那个约定时,你就会知道它是事件。 因此,如果我稍微重构您的代码,它应该是这样的:

      在你的类库中:

      //...
      public class YourEventArgs : EventArgs
      {
         public string Data { get; set; }
      }
      //...
      
      public event EventHandler DataReceived = null;
      ...
      protected override OnDataReceived(object sender, YourEventArgs e)
      {
         if(DataReceived != null)
         {
            DataReceived(this, new YourEventArgs { Data = "data to pass" });
         }
      }
      

      当您的类库想要启动事件时,它应该调用 OnDataReceived,它负责检查是否有人在监听并构造适当的 EventArgs 以将您的数据传递给监听器。

      在应用程序中你应该改变你的方法签名:

      Library.Class a = new Library.Class();
      a.DataReceived += ReceivedData;
      a.start(ip, port);
      
      //...
      
      void ReceivedData(object sender, YourEventArgs e)
      {
        string data = e.Data;
        //...
      }
      

      【讨论】:

      • +1 表示“.NET 正在以这种方式使用事件”,并提及 Action 答案。
      【解决方案3】:

      为您的班级添加活动

      public event DataReceivedEventHandler DataReceived;
      public delegate void DataReceivedEventHandler(object sender, SocketDataReceivedEventArgs e);
      

      创建一个包含所需参数的类,例如 Ex : SocketDataReceivedEventArgs here

      触发事件,如

      SocketDataReceivedEventArgs DataReceiveDetails = new SocketDataReceivedEventArgs();
      DataReceiveDetails.data = "your data here";
      DataReceived(this, DataReceiveDetails);
      

      在应用程序创建方法中

      void receivedData(object sender, SocketDataReceivedEventArgs e)
      {
          // e.data is data which received by library....
      }
      

      现在将处理程序附加到它

       Library. Class a = new Library. Class();
          a.DataReceived += receivedData;
          a.start(ip, port);
      

      您需要根据您的要求在多个线程中编写它 以下是如何在上面添加线程安全支持

      Dispatcher.Invoke and accessing textbox from another thread

      【讨论】:

      • 使用委托与仅使用事件有什么不同?
      猜你喜欢
      • 2012-06-17
      • 1970-01-01
      • 2018-01-28
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多