C# 如何在c中生成从库到应用程序的回调(事件)#
我正在开发一个库(DLL),在这个库中,我需要向用户提供事件(中断),作为一种数据方法。库的工作是开始在套接字上列出,从套接字接收数据,并用一种方法将这些数据传递给用户 库:C# 如何在c中生成从库到应用程序的回调(事件)#,c#,oop,events,dll,C#,Oop,Events,Dll,我正在开发一个库(DLL),在这个库中,我需要向用户提供事件(中断),作为一种数据方法。库的工作是开始在套接字上列出,从套接字接收数据,并用一种方法将这些数据传递给用户 库: public void start(string IP, int port) { // start logic... // receives data from socket... send this data to user } Library. Class a = new Library. Cla
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....
}
应用程序:
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....
}
如何使用库中的数据向应用程序引发事件
提前感谢….您应该更改开始方法的签名以传递给该代理:
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....
}
public void start(字符串IP、int端口、操作回调)
{
//启动逻辑。。。
//从套接字接收数据…将此数据发送给用户
回调(数据);
}
图书馆。a类=新图书馆。类();
a、 启动(ip、端口、接收数据);
//我需要库在接收数据时自动调用此方法。。。
无效接收数据(字符串数据)
{
//图书馆收到的数据。。。。
}
将事件添加到库中,如下所示:
public event Action<string> OnDataReceived = null;
就这样
但您可能希望使用约定来编写事件,我建议您开始使用它,因为.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;
//...
}
将事件添加到类中
public event DataReceivedEventHandler DataReceived;
public delegate void DataReceivedEventHandler(object sender, SocketDataReceivedEventArgs e);
在此处创建一个包含所需参数的类,例如Ex:SocketDataReceivedEventArgs
触发事件类
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);
根据需要,您需要在多个线程中编写它
下面是您如何向上面添加线程安全支持的方法
+1 for“.NET正以这种方式使用事件”,并提及
操作
回答。使用委托与仅使用事件有什么不同?