. NET Framework | notes | procedures | design | Example 11th multi-event example [Story of a man and three women]
Summary:
Applying the System.ComponentModel.EventHandlerList example in the FCL application of a type to publish multiple events
Scene: A boy has three girlfriends, each have different hobbies, girlfriend a love music, girlfriend B Love food, girlfriend C love xxx, to meet the various girlfriends, this boy must sing, cooking food, xxx.
This production program demonstrates the application of a single type of multiple events, and assumes that the man can only do one thing at a time (that is, to rule out the possibility of cooking on the side of xxx while singing or side xxx)
The following is the source code:
Using System;
Using System.ComponentModel;
Boyfriend's source code
public class boyfriend
{
Protected EventHandlerList EventList
= new EventHandlerList ();
//
Meet girlfriend a define music preferences
Using custom music events and callback functions
Protected static ReadOnly Object Musiceventkey = new Object ();
public class Musiceventargs:eventargs
{
private string Strmusicname;
public string Musicname
{
get{
return strmusicname;
}
}
Public Musiceventargs (String strmusicname)
{
This.strmusicname = Strmusicname;
}
}
public delegate void Musiceventhandler (object sender, Musiceventargs args);
Public event Musiceventhandler Musicmsg
{
Add
{
Eventlist.addhandler (Musiceventkey, value);
}
Remove
{
Eventlist.removehandler (Musiceventkey, value);
}
}
protected virtual void Onmusic (Musiceventargs e)
{
Delegate d = Eventlist[musiceventkey];
if (d!= null)
{
D.dynamicinvoke (New Object[]{this, E});
}
}
public void Simulatemusic (string strName)
{
Console.WriteLine ("Boyfriend: OK, I'll sing you a {0}!") ", StrName);
Onmusic (New Musiceventargs (StrName));
}
//
Meet girlfriend B's gourmet Desire
//
Protected static ReadOnly Object Cateeventkey = new Object ();
public class Cateeventargs:eventargs
{
private string Strcatename;
public string Catename
{
Get
{
return strcatename;
}
}
Public Cateeventargs (String strcatename)
{
This.strcatename = Strcatename;
}
}
public delegate void Cateeventhandler (Object sender, Cateeventargs args);
Public event Cateeventhandler Catemsg
{
Add
{
Eventlist.addhandler (Cateeventkey, value);
}
Remove
{
Eventlist.removehandler (Cateeventkey, value);
}
}
protected void Oncate (Cateeventargs e)
{
Delegate d = Eventlist[cateeventkey];
if (d!= null)
{
D.dynamicinvoke (New Object[]{this, E});
}
}
public void Simulatecate (string strcatename)
{
Console.WriteLine ("Boyfriend: please eat a little I do {0}", strcatename);
Oncate (New Cateeventargs (Strcatename));
}
//
satisfy girlfriend C's XXX Desire
Using the Eventargs.empty event and the System.EventHandler callback function
Protected static ReadOnly Object Xxxeventkey = new Object ();
Public event EventHandler Xxxmsg
{
Add
{
Eventlist.addhandler (Xxxeventkey, value);
}
Remove
{
Eventlist.removehandler (Xxxeventkey, value);
}
}
protected virtual void OnXXX ()
{
Delegate d = Eventlist[xxxeventkey];
if (d!= null)
{
D.dynamicinvoke (New Object[]{this, eventargs.empty});
}
}
public void Simulatexxx ()
{
Console.WriteLine ("Boyfriend: you are so beautiful today!");
OnXXX ();
}
public static void Main ()
{
Boyfriend bf = new Boyfriend ();
//
Console.WriteLine ("Morning girlfriend A To play:");
Gf_a GFA = new gf_a (BF);
Bf. Simulatemusic ("Love Song");
Gfa. Unregister (BF);
//
Console.WriteLine ();
Console.WriteLine ("Afternoon girlfriend B to play");
Gf_b GFB = new Gf_b (BF);
Bf. Simulatecate ("Ancestral small dessert");
Gfb. Unregister (BF);
//
Console.WriteLine ();
Console.WriteLine ("Evening girlfriend C to play");
Gf_c GFC = new Gf_c (BF);
Bf. Simulatexxx ();
Gfc. Unregister (BF);
}
}
Girlfriend A's source code
public class Gf_a
{
Public gf_a (boyfriend BF)
{
Bf. Musicmsg + = new Boyfriend.musiceventhandler (musicmsg);
Console.WriteLine ("Girlfriend A: Husband! I want to listen to the song");
Console.WriteLine ("Girlfriend C:r ...") O... O... M.. ");
}
public void unregister (boyfriend BF)
{
EventHandler e = new EventHandler (xxxmsg);
Bf. Xxxmsg = e;
Console.WriteLine ("Girlfriend C: tired, want to rest!");
}
}
/* Run Results:
Morning girlfriend A to play:
Girlfriend A: Honey, I want to hear a song.
Boyfriend: OK, I'll Sing you a love song!
Girlfriend A: Wow, it's love song Yes, good love!
Girlfriend A: Have a rest, don't fight!
Afternoon girlfriend B to play
Girlfriend B: Hubby, I'm hungry!
Boyfriend: Please have some of my heirloom cookies.
Girlfriend B: Wow! Honey, you are so good, the ancestral dessert is delicious!
Girlfriend B: Full, thank you Oh!
Night girlfriend C to play
Girlfriend C: Husband! You look so handsome today!
Boyfriend: You are so beautiful today.
Girlfriend C:r ... O... O... M...
Girlfriend C: Tired, want to have a rest!
*/
Note: 1, because the above example uses the System.ComponentModel.EventHandlerList in FCL, therefore does not have the thread security.
2. The xxx section of the above code does not define event parameters but uses System.EventArgs.Emtpy, and does not define callback functions but uses system.eventhandler; the other two events are custom. You can modify two other events
3. For more information on publishing events, custom events, and multiple event definitions, refer to the. NET Framework Program Design Reading notes _ Chapter 11th events
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.