多播委托如何异步并发执行

应用场景需求:

      小孩叫“我饿了”,这时需要他爸爸和妈妈同时听到并同时进行处理,小孩不能等他爸妈回应后再闭嘴(他爸妈的处理不能阻塞小孩说完话后闭嘴)。

解决方法:

      通常使用“多播异步委托”可以实现一个“观察者模式”达到大部分要求,但多播委托是顺序执行的,所以他爸妈不能同时处理。没有查找到“多播异步并发委托”的实现例子,好像委托也不支持并发执行。于是改用“异步委托列表”的方式达到“多播异步并发委托”的效果。

代码如下:

using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  观察者模式
{
    
public   delegate   void  SpeakDelegate( string  speak);

    
class  Child
    {

        
public  List < SpeakDelegate >  SpeakHandleList  =   new  List < SpeakDelegate > ();

        
public   void  Speak( string  speak)
        {
            
foreach  (SpeakDelegate SpeakHandle  in   this .SpeakHandleList)
            {
                
if  (SpeakHandle  !=   null )
                    SpeakHandle.BeginInvoke(speak, FinishListion, 
null );
            }
        }

        
private   void  FinishListion(IAsyncResult result)
        {
           
        }

    }

}

 

 

 

 

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;

namespace  观察者模式
{

    
public   partial   class  Form1 : Form
    {
        
private   delegate   void  UpdateUIDelegate( string  text);
        
private  Child m_Child;


        
public  Form1()
        {
            InitializeComponent();
        }

        
private   void  Form1_Load( object  sender, EventArgs e)
        {
            
this .m_Child  =   new  Child();
       

            SpeakDelegate AHandle 
=   new  SpeakDelegate( this .Parent_ListenA);
            
this .m_Child.SpeakHandleList.Add(AHandle);

            SpeakDelegate BHandle 
=   new  SpeakDelegate( this .Parent_ListenB);
            
this .m_Child.SpeakHandleList.Add(BHandle);


        }

        
private   void  button1_Click( object  sender, EventArgs e)
        {
            
this .AddText( " 开始\r\n " );
            
this .m_Child.Speak( this .textBox1.Text);
            
this .AddText( " 结束\r\n " );
        }

        
public   void  Parent_ListenA( string  speak)
        {
            
this .AddText( "   爸爸听到了,等着吧....\r\n " );
            System.Threading.Thread.Sleep(
1000 );
            
this .AddText( "   爸爸回复:等会解决了这个问题给你买雪糕!\r\n " );

        }

        
public   void  Parent_ListenB( string  speak)
        {
            
this .AddText( "   妈妈听到了,等着吧....\r\n " );
            System.Threading.Thread.Sleep(
5000 );
            
this .AddText( "   妈妈回复:让你爸给你买去!\r\n " );
        }

        
private   void  AddText( string  text)
        {
            
if  ( this .InvokeRequired)
            {
                UpdateUIDelegate d 
=   new  UpdateUIDelegate(AddText);
                
this .Invoke(d,  new   object [] { text });

            }
            
else
            {
                
this .textBox2.Text  +=  DateTime.Now.ToString()  +  text;
            }

        }

    }
}

 

 

 

 运行结果:

从执行结果看 前两行表明了同步,三四行表明了并发。

 

斗胆发在首页候选区,希望能有人提出问题的其它解决方案。

 代码

转载于:https://www.cnblogs.com/zhaobl/archive/2010/02/03/1662855.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值