Never Quit
Never Quit

Reputation: 2082

How to generate callback (event) from library to application in c#

I'm developing one library (DLL), in which I need to provide event (interrupt) to user as one method with data. Library's work is start listing on socket, receive data from socket and pass this data to user in one method.

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 event to application with data from library?

Thanks in advance....

Upvotes: 10

Views: 8597

Answers (3)

Jack Gajanan
Jack Gajanan

Reputation: 1670

Add event to your class

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

Create a class that contents your required parameters like Ex : SocketDataReceivedEventArgs here

Trigger event like

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

in application create method

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

Now attach handler to it

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

You need to write it in multiple threads as your requirement is Here is how you can add thread safe support to above

Dispatcher.Invoke and accessing textbox from another thread

Upvotes: 0

Shahar G.
Shahar G.

Reputation: 1510

Add an event to your library like this:

public event Action<string> OnDataReceived = null;

Then, in Application:

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

That's it.

But you may want to write events with the conventions and I suggest you'll start get use to it because .NET is using events that way so whenever you bump into that convention you'll know it's events. So if I refactor your code a little bit 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 launch the event it should call the OnDataReceived which is responsible for checking that someone is listening and constructing the appropriate EventArgs for passing by your data to the listener.

In the Application you should 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;
  //...
}

Upvotes: 18

Kirill Bestemyanov
Kirill Bestemyanov

Reputation: 11964

You should change signature of start method to pass there 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....
}

Upvotes: 2

Related Questions