广播消息

1. 接收消息和广播消息的类

<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MSGNotify.AppCode
{
    enum EventType
    {
        FireByA,
    }

    class BoardCast
    {
        private Dictionary<EventType ,int> m_Send = new Dictionary<EventType ,int>();
        private Dictionary<EventType, List<Action<int>>> m_Receive = new Dictionary<EventType, List<Action<int>>>();

        public void Init()
        {
            
        }

        private static BoardCast instance = new BoardCast();
        public static BoardCast Instance { get { return instance; } }

        public void RegisterGetFireEvent(EventType type, Action<int> _action)
        {
            if (!m_Receive.ContainsKey(type))
            {
                KeyValuePair<EventType, List<Action<int>>> pair = new KeyValuePair<EventType, List<Action<int>>>(type, new List<Action<int>>());
                m_Receive.Add(pair.Key, pair.Value);
            }

            m_Receive[type].Add(_action); 
        }

        public void RegisterOnFireEvent(EventType type, int msg)
        {
            m_Send[type] = msg;
        }

        public void ProcessEvent()
        {
            foreach (KeyValuePair<EventType,int> pair in m_Send)
            {
                List<Action<int>> actionList = new List<Action<int>>();
                m_Receive.TryGetValue(pair.Key, out actionList);

                foreach (Action<int> action in actionList)
                {
                    action.Invoke(pair.Value);
                }
            }

            m_Send.Clear();
        }
    }

}</span>

2. 测试类A 和B,A广播消息,B接收消息

<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MSGNotify.AppCode
{
    class A
    {
        public int x = 1;

        public A()
        {
           // Register();
        }

        public void Set(int _x)
        {
            x = _x;
            BoardCast.Instance.RegisterOnFireEvent(EventType.FireByA, x);
        }

        public void Print(int i)
        {
            Console.WriteLine("A{0}:{1}", i, x);
        }
    }
}
</span>

3. main测试

<span style="font-size:18px;">using MSGNotify.AppCode;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MSGNotify
{
    class Program
    {
        static void Main(string[] args)
        {
            //A a1 = new A();
            //A a2 = new A();

            //a1.Set(10);
            //a2.Set(20);
            A a = new A();
            B b = new B();

            BoardCast.Instance.ProcessEvent();
            Console.WriteLine("A never changed,A:{0}",a.x);
            //BoardCast.Instance.Process();

            a.Set(10);
            BoardCast.Instance.ProcessEvent();
        }
    }
}
</span>
4. 运行结果

5. 代码

http://download.csdn.net/detail/ywjun0919/7990735

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值