Search code examples
c#eventsevent-handlingeventhandler

C# Event Handling - Event Handler remains null


I am a beginner to C# programming.
When I run this program, the eventhandler named - newMessagePublishedEventHandler of the class MessagePool is always null while executing the OnNewMessagePublished() function of the same class.
Can anyone please tell me why? and also how could I resolve this?

namespace ConsoleApp
{
public class MessageEventArgs : System.EventArgs
{
    private int publisherId;
    private string message;

    public MessageEventArgs(int publisherId, string messageText)
    {
        this.publisherId = publisherId;
        this.message = messageText;
    }

    public int PublisherId
    {
        get { return publisherId; }
    }

    public string Message
    {
        get { return message; }
    }
}

public class Publisher
{
    private int publisherId;

    public Publisher(int publisherId)
    {
        this.publisherId = publisherId;
    }

    public int PublisherId
    {
        get { return publisherId; }
    }

    public void Publish(string message)
    {
        MessagePool.GetInstance().Publisher_PostMessage(this.publisherId, message);
        Console.WriteLine("published message - " + message);
    }
}

public class MessagePool
{
    private static MessagePool theOnlyInstanceOfMessageBroker;

    public static MessagePool GetInstance()
    {
        if (theOnlyInstanceOfMessageBroker == null)
        {
            return new MessagePool();
        }
        else
        {
            return theOnlyInstanceOfMessageBroker;
        }
    }

    private MessagePool()
    {
    }

    private EventHandler newMessagePublishedEventHandler;

    public event EventHandler NewMessagePublished
    {
        add
        {
            newMessagePublishedEventHandler += value;
        }

        remove
        {
            newMessagePublishedEventHandler -= value;
        }
    }

    public void Publisher_PostMessage(int publisherId, string message)
    {
        DateTime publishedTime = DateTime.Now;
        this.OnNewMessagePublished(publisherId, message);
    }

    private void OnNewMessagePublished(int publisherId, string message)
    {
        MessageEventArgs eventArgs = new MessageEventArgs(publisherId, message);
        if (newMessagePublishedEventHandler != null)
        {
            newMessagePublishedEventHandler(this, eventArgs);
        }
    }
}

public class Subscriber
{
    private int subscriberId;

    public Subscriber(int subscriberId)
    {
        this.subscriberId = subscriberId;
        this.SubscribeToMessagebroker();
    }

    public int SubscriberId
    {
        get { return subscriberId; }
    }

    private void SubscribeToMessagebroker()
    {
        MessagePool.GetInstance().NewMessagePublished -= Subscriber_NewMessageArrived;
        MessagePool.GetInstance().NewMessagePublished += Subscriber_NewMessageArrived;
    }

    private void Subscriber_NewMessageArrived(object sender, EventArgs eventArgs)
    {
        if (eventArgs != null && eventArgs is MessageEventArgs)
        {
            var data = eventArgs as MessageEventArgs;
            if (data != null)
                Console.WriteLine("Recieved message : '" + data.Message + "' from " + data.PublisherId);
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        Subscriber subscriber = new Subscriber(1);
        Publisher publisher = new Publisher(1001);
        publisher.Publish("Hey man.. whats up?");
        Console.ReadLine();
    }
}
}

Solution

  • MessagePool is not a proper Singleton, you are always returning a new MessagePool.

        public class MessagePool
        {
            private static MessagePool theOnlyInstanceOfMessageBroker;
            private static object _syncRoot = new object();
    
            public static MessagePool GetInstance()
            {
                if (theOnlyInstanceOfMessageBroker == null)
                {   
                    lock(_syncRoot)
                    {
                        if (theOnlyInstanceOfMessageBroker == null) 
                            theOnlyInstanceOfMessageBroker = new MessagePool();
                    }
                }
    
                return theOnlyInstanceOfMessageBroker;
    
            }
    
            private MessagePool()
            {
            }
    
    //...
    }