C# 事件处理 - 事件处理程序保持为空



我是C#编程的初学者。
当我运行这个程序时,在执行同一类的OnNewMessagePublished()函数时,类MessagePool的名为 - newMessagePublishedEventHandler的事件处理程序始终为 null。
谁能告诉我为什么?还有我该如何解决这个问题?

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();
    }
}
}
MessagePool不是

合适的单例,您总是返回一个新的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()
        {
        }
//...
}

相关内容

  • 没有找到相关文章

最新更新