Chapter 11 Multi-Event Examples [A Man and Three Women's Story]
?
Summary:
?????? Apply the application of the system.componentmodel.eventhandlerlistModel.EventHandlerList example in one type to publish multi-event applications
?
Scenario: A boy has three girlfriends, each has different hobbies, girlfriend a hobby music, girlfriend B love food, girlfriend C hobby XXX, to meet all girlfriends, this boy must sing, cook food, xxx.
By this make program demonstration order type multi-event application, and assume that this man can only do one thing (ie, the possibility of singing or singing or singing while XXX J)
Such as the source code:
Using system;
Using system.componentmodel;
?
// boyfriend's source code
Public Class Boyfriend
{
?????? protected EventHandlerList EventList
????????????????????? = new EventHandlerList ();
?
?????? //
?????? // Meet my girlfriend a definition music preferences
?????? // use 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 musiceTargs (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];
????????????? ing (d! = null)
????????????? {
????????????????????? d.Dynamicinvoke (new object [] {this, e}); ??????????????? ?
??????}
??????
?????? public void simulatemusic (String Strname)
?????? {
????????????? console.writeline ("Boyfriend: Ok, I will sing {0}!", Strname;
?????? ?????? onmusic (new musicEventArgs (STRNAME);
?????}
?????
????? //
????? // Meet the gatherings of girlfriends B
????? //
????? 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 cateeventage (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];?
????????????? ing (d! = null)
????????????? {
????????????????????? D. DynamicInvoke (new object [] {this, e});???
?????????????}
??????}
?????? public void simulatecate (String strcatename)
?????? {
????????????? console.writeline ("Boyfriend: Please eat a little {0}", strcatename);
????????????? oncate (new cateeventargs (strcatename);
??????} ??????
?????? //
?????? // Meet the XXX desire of girlfriend C
?????? // use Eventargs.empty events and System.EventHandler callback functions
?????? protected ?e = new object ();
?????? public evenet even xxxmsg
?????? {
????????????? add
????????????? {
???????????????????? EventList.AddHandler (XXXEventKey, Value);
?????????????}???
????????????? remove
????????????? {
???????????????????? EventList.removeHandler (XXXEventKey, Value); ??
?????????????}
??????}
?????? protected virtual void onxxx ()
?????? {
????????????? delegate D = EventList [xxxeventKey];
????????????? ing (d! = null)
????????????? {
????????????????????? D. DynamicInvoke (new object [] {this, evenetargs.empty});
?????????????}
??????}
?????? public void simulatexxx ()
?????? {
?????????????? console.writeline ("Boyfriend: You are so beautiful today!"); ?????
????????????? ONXXX ();
??????}
??????
?????? public static void
Main
()
?????? {
????????????? boyfriend bf = new boyfriend ();
?????????????
????????????? //
????????????? console.writeline ("AM 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 is playing");
????????????? GF_B GFB = New GF_B (BF);
????????????? bf.simulatecate ("ancestral small dessert");
????????????? gfb.unregister (bf);
?????????????
????????????? //
????????????? console.writeLine ();
????????????? console.writeline ("Good night girlfriend C is playing");
????????????? GF_C GFC = New GF_C (BF);
????????????? bf.simulatexxx ();
????????????? gfc.unregister (bf);
??????}
}
?
// girlfriend a 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 songs");
??????}
?????????????
?????? private void musicmsg (Object sender, Boyfriend.MusiceventArgs args)
?????? {
????????????? switch (args.musicname)
????????????? {
???????????????????? Case "love song":
???????????????????? case "clear song": ?????????????????????????????????????????????????
?????????????????????????????????????????????????????????? console.writeline ("Girlfriend A: Wow, is {0}, I like it!" Args.MusicName);
???????????????????????????
???????????????????? default:
??????????????????????????????????????????????????????????????????????????????????????????????????????????????: 这 这 没 没 耶 耶 耶 过 过 过 过 过 过 过 过 过 过;
???????????????????????????
?????????????}
??????}
??????
?????? public void unregister (Boyfriend BF)
?????? {
?????????????? boyfriend.musiceventhandler bfe = new boyfriend.musicEventhandler (Musicmsg);
????????????? bf.musicmsg - = bfe;
????????????? console.writeline ("Girlfriend A: Rest, don't noise!");
??????}
}
// girlfriend B source code
Public Class GF_B
{
?????? public GF_B (Boyfriend BF)
?????? {
????????????? bf.catemsg = new boyfriend.cateeventhandler (catemsg);
?????????????? console.writeline ("Girlfriend B: Husband! I am hungry!"); ??????
??????}
?????? private vid catemsg (Object sender, BoyFriend.cateEventArgs args)
?????? {
????????????? switch (args.catename)
????????????? {
????????????????????? case "ancestral small dessert":
????????????????????????????????????? console.writeline ("Girlfriend B: Wow! Husband, you can do it, {0} eat well ! ", Args.catename;
???????????????????????????
???????????????????? Case "biscuit":
???????????????????? Case "instant noodles":
??????????????????????????????????????????????????????????????????????????????????? console.writeline ("Girlfriend B: When you know you, give people a small snack now, now people Eating convenience, 555555 "); ????????????????????????????????????????????????
???????????????????? default:
??????????????????????????????????????????????????????????????????????????????????
???????????????????????????
?????????????}???
??????}
?????? public void unregister (Boyfriend BF)
?????? {
????????????? BoyFriend.cateEventHandler E = New BoyFriend.cateEventHandler (CATEMSG);
????????????? bf.catemsg - = E;
????????????? console.writeline ("girlfriend B: full, thank you!");
??????}
}
// girlfriend C source code
Public Class GF_C
{
?????? public GF_C (Boyfriend BF)
?????? {
????????????? bf.xxxmsg = new eventhandler (xxxmsg);
?????????????? console.writeline ("girlfriend C: husband! You are really handsome today!");
??????}
??????
?????? private void xxxmsg (Object sender, Eventargs args)
?????? {
????????????? console.writeline ("Girlfriend C: r ... o ... m ...");
??????}
??????
?????? public void unregister (Boyfriend BF)
?????? {
????????????? EventHandler E = New EventHandler (XXXMSG);
?????????????? bf.XXXXMSG-= E;
?????????????? console.writeline ("girlfriend C: tired, want to rest!");??
??????}
}
/*operation result:
? Morning girlfriend a to play:
Girlfriend A: Husband! I want to listen to songs
Boyfriend: Ok, I will sing a love song!
Girlfriend A: Wow, is love song, so I like it!
Girlfriend A: Rest, don't be noisy!
Afternoon girlfriend b
Girlfriend B: Husband! I am hungry!
Boyfriend: Please eat a little dessert me.
Girlfriend B: Wow! Husband, you can really do it, and the ancestors is a little dessert.
Girlfriend B: Eat, thank you!
Night girlfriend C
Girlfriend C: Husband! You are really handsome today!
Boyfriend: You are so beautiful today!
Girlfriend C: r ... o ... o ... m ...
Girlfriend C: Tired, want to rest!
* /
Note: 1. Because of the case of system.componentmodel.eventHandlerlist in the FCL, thread security is not available.
?????? 2, the XXX section in the above code is not defined to use System.Eventargs.emtpy, nor does the callback function are defined but use System.EventHandler; other two events are customized. You can modify the other two events ?????? 3, for details on issuance events, custom events, multi-event definitions, please refer to ".NET Framework Design" Reading Note _ Chapter 11 Event