Asynchronous and concurrent execution of multicast Delegation

Source: Internet
Author: User

Application Scenario requirements:

The child is called "I Am Hungry". At this time, his father and mother need to hear and handle the problem at the same time, A child cannot shut up after his parents respond (his/her mom's handling cannot block the child from shutting up after speaking ).

Solution:

Generally, "Multicast asynchronous delegation" can be used to implement an "Observer mode" to meet most of the requirements. However, multicast delegation is executed in sequence, so his parents cannot process it at the same time. The implementation example of "Multicast asynchronous concurrent delegation" is not found, as if the delegation does not support concurrent execution. Therefore, the "Asynchronous delegation list" method is used to achieve the "Multicast asynchronous concurrent delegation" effect.

The Code is as follows:

Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace observer Mode
{
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 observer Mode
{

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 button#click (object sender, EventArgs e)
{
This. AddText ("Start \ r \ n ");
This. m_Child.Speak (this. textBox1.Text );
This. AddText ("End \ r \ n ");
}

Public void Parent_ListenA (string speak)
{
This. AddText ("dad heard it, wait... \ r \ n ");
System. Threading. Thread. Sleep (1000 );
This. AddText ("dad replied: I will solve this problem and buy ice cream for you! \ R \ n ");

}

Public void Parent_ListenB (string speak)
{
This. AddText ("mom heard it, wait... \ r \ n ");
System. Threading. Thread. Sleep (5000 );
This. AddText ("Mom replied: let your dad buy it for you! \ 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;
}

}

}
}

 

 

 

Running result:

From the execution result, the first two rows indicate synchronization, and the third and fourth rows indicate concurrency.

 

I am bold in the candidate area on the homepage, hoping that other solutions to the problem can be raised.

Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.