应用场景需求:
小孩叫“我饿了”,这时需要他爸爸和妈妈同时听到并同时进行处理,小孩不能等他爸妈回应后再闭嘴(他爸妈的处理不能阻塞小孩说完话后闭嘴)。
解决方法:
通常使用“多播异步委托”可以实现一个“观察者模式”达到大部分要求,但多播委托是顺序执行的,所以他爸妈不能同时处理。没有查找到“多播异步并发委托”的实现例子,好像委托也不支持并发执行。于是改用“异步委托列表”的方式达到“多播异步并发委托”的效果。
代码如下:
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.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;
}
}
}
}
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;
}
}
}
}
运行结果:
从执行结果看 前两行表明了同步,三四行表明了并发。
斗胆发在首页候选区,希望能有人提出问题的其它解决方案。