How to create a callback (event) from a library to an application in C #

I am developing a single library (DLL) in which I need to provide an event (interrupt) to the user as one method with data. Working with a library means launching a list of sockets, receiving data from a socket, and transferring this data to the user in one way.

Library:

public void start(string IP, int port) { // start logic... // receives data from socket... send this data to user } 

Application:

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

How to raise an event in an application with data from the library?

Thanks in advance....

+6
source share
3 answers

Add the event to your library as follows:

 public event Action<string> OnDataReceived = null; 

Then in the application:

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

What is it.

But you can write events with conventions, and I suggest you start using it, because .NET uses events in this way, so whenever you come across this convention, you will know its events. Therefore, if I reorganize your code a little, it should be something like:

In your class library:

 //... 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" }); } } 

When your class library wants to fire an event, it must call OnDataReceived, which is responsible for checking that someone is listening and creating the appropriate EventArgs to pass your data to the listener.

In the application, you must change your method signature:

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

You must change the signature of the start method to pass the delegate:

 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.... } 
+1
source

Add an event to your class

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

Create a class that contains your required parameters, such as Ex: SocketDataReceivedEventArgs here

Trigger event e.g.

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

in the application creation method

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

Now attach a handler to it

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

You need to write it to multiple streams, as your requirement is Here's how you can add streaming security support above

Dispatcher.Invoke and access to a text field from another thread

0
source

Source: https://habr.com/ru/post/944754/


All Articles