Hope I phrased the question correctly, feel free to give suggestions in order to make it more clear.
I basically just need to make this function async:
protected virtual void OnNotificationReceived(Publisher p, NotificationEvent e)
Not sure how to modify the rest of the code to make it work, please help.
Publisher class
class Publisher{
//publishers name
public string PublisherName { get; private set; }
//publishers notification interval
public int NotificationInterval { get; private set; }
// declare a delegate function
public delegate void Notify(Publisher p, NotificationEvent e);
// declare an event variable of the delegate function
public event Notify OnPublish;
// class constructor
public Publisher(string _publisherName, int _notificationInterval){
PublisherName = _publisherName;
NotificationInterval = _notificationInterval;
}
//publish function publishes a Notification Event
public void Publish(){
while (true){
// fire event after certain interval
Thread.Sleep(NotificationInterval);
if (OnPublish != null)
{
NotificationEvent notificationObj = new NotificationEvent(DateTime.Now, "New Notification Arrived from");
OnPublish(this, notificationObj);
}
Thread.Yield();
}
}
}
Subscriber class
class Subscriber{
public string SubscriberName { get; private set; }
public Subscriber(string _subscriberName){
SubscriberName = _subscriberName;
}
// This function subscribe to the events of the publisher
public void Subscribe(Publisher p){
// register OnNotificationReceived with publisher event
p.OnPublish += OnNotificationReceived; // multicast delegate
}
// This function unsubscribe from the events of the publisher
public void Unsubscribe(Publisher p){
// unregister OnNotificationReceived from publisher
p.OnPublish -= OnNotificationReceived; // multicast delegate
}
// It get executed when the event published by the Publisher
// ***I want to make this method async here:***
protected virtual void OnNotificationReceived(Publisher p, NotificationEvent e){
Console.WriteLine("Hey " + SubscriberName + ", " + e.NotificationMessage +" - "+ p.PublisherName + " at " + e.NotificationDate);
}
}