Tutorials to .com

Tutorials to .com » Dotnet » Database » A man and three woman's story [ ". Net framework for program design," notes study multi-chapter event _ XI sample]

A man and three woman's story [ ". Net framework for program design," notes study multi-chapter event _ XI sample]

Print View , by: iSee ,Total views: 9 ,Word Count: 1608 ,Date: Sun, 7 Jun 2009 Time: 8:36 PM

Chapter XI multi-event sample [a man and three women's stories]

Abstract:

FCL application example of a type System.ComponentModel.EventHandlerList release events in the application of



Scene: A girlfriend of three boys, each of which has different hobbies, girlfriend A loving music, food-loving girlfriend B, C-loving girlfriend XXX, to meet the various girlfriends, the boys must be singing, cooking food, xxx.

Demonstration of the production process as a single type of multi-application events, and assuming that this man can only do one thing at the same time (that is, to exclude one side of xxx or xxx side while singing the possibility of cooking J)

For the source code is as follows:

using System;

using System.ComponentModel;



/ / Boyfriend of the source code

public class BoyFriend

(

protected EventHandlerList eventList

= New EventHandlerList ();



/ /

/ / Meet the definition of girlfriend A music preferences

/ / Use a 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: Yes, I will sing to you now (0)!", StrName);

OnMusic (new MusicEventArgs (strName));

)



/ /

/ / Satisfy the desire for food girlfriend 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 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 I do eat (0)", strCateName);

OnCate (new CateEventArgs (strCateName));

)



/ /

/ / Satisfy the desire for a girlfriend C, xxx

/ / Use EventArgs.Empty events and callback functions System.EventHandler

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: Oh you really beautiful!");

OnXXX ();

)



public static void Main ()

(

BoyFriend bf = new BoyFriend ();



/ /

Console.WriteLine ( "morning girlfriend A play:");

GF_A gfa = new GF_A (bf);

bf.SimulateMusic ( "Love");

gfa.Unregister (bf);



/ /

Console.WriteLine ();

Console.WriteLine ( "the afternoon of his girlfriend to play B");

GF_B gfb = new GF_B (bf);

bf.SimulateCate ( "ancestral small dessert");

gfb.Unregister (bf);



/ /

Console.WriteLine ();

Console.WriteLine ( "girlfriend c at night to play");

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");

)



private void MusicMsg (Object sender, BoyFriend.MusicEventArgs args)

(

switch (args.MusicName)

(

case "Love":

case "Qing Song":

Console.WriteLine ( "girlfriend A: wow, is (0) ye, ah well like!", Args.MusicName);

break;

default:

Console.WriteLine ( "girlfriend A: The song heard ye, good to listen to O!");

break;

)

)



public void Unregister (BoyFriend bf)

(

BoyFriend.MusicEventHandler bfe = new BoyFriend.MusicEventHandler (MusicMsg);

bf.MusicMsg -= bfe;

Console.WriteLine ( "girlfriend A: rest, and!");

)

)

/ / Girlfriend B's source code

public class GF_B

(

public GF_B (BoyFriend bf)

(

bf.CateMsg + = new BoyFriend.CateEventHandler (CateMsg);

Console.WriteLine ( "girlfriend B: husband! I'm hungry!");

)

private void CateMsg (Object sender, BoyFriend.CateEventArgs args)

(

switch (args.CateName)

(

case "ancestral small dessert":

Console.WriteLine ( "girlfriend B: wow! Husband you really competent, (0) good taste ye!", Args.CateName);

break;

case "cookies":

case "instant noodles":

Console.WriteLine ( "girlfriend B: just to know you, the small sum to the people, let people eat convenience foods, and 555555");

break;

default:

Console.WriteLine ( "girlfriend B: What is the Eastern, not eaten Ye");

break;

)

)

public void Unregister (BoyFriend bf)

(

BoyFriend.CateEventHandler e = new BoyFriend.CateEventHandler (CateMsg);

bf.CateMsg -= e;

Console.WriteLine ( "girlfriend B: nothing, thank you Oh!");

)

)

/ / Girlfriend C++ source code

public class GF_C

(

public GF_C (BoyFriend bf)

(

bf.XXXMsg + = new EventHandler (XXXMsg);

Console.WriteLine ( "a girlfriend C: husband!really you!");

)



private void XXXMsg (Object sender, EventArgs args)

(

Console.WriteLine ( "a girlfriend C: R. .. O. .. O. .. M. ..");

)



public void Unregister (BoyFriend bf)

(

EventHandler e = new EventHandler (XXXMsg);

bf.XXXMsg -= e;

Console.WriteLine ( "a girlfriend C: tired, like the rest!");

)

)

/ * Results:

A girlfriend the morning of play:
Girlfriend A: husband! I would like to listen
Boyfriend: Yes, I will sing to you Love it!
Girlfriend A: wow, is Love ye, good like ah!
Girlfriend A: rest, and!

B girlfriend play this afternoon
Girlfriend B: husband! I'm hungry!
Boyfriend: Please point me to eat a small dessert of ancestral
Girlfriend B: wow! You really competent husband, a good dessert delicious small ancestral Ye!
Girlfriend B: nothing, thank you Oh!

C at night to play his girlfriend
Girlfriend C: husband! You reallytoday!
Boyfriend: Oh you really beautiful!
Girlfriend C: R. .. O. .. O. .. M. ..
Girlfriend C: tired, like the rest!

* /

Note: 1, due to the above example of the use of FCL in System.ComponentModel.EventHandlerList, therefore do not have the security thread.

2, the code is not defined in the xxx part of the incident but the use of parameters System.EventArgs.Emtpy, there is no definition of a callback function but using System.EventHandler; the other two events are customizable. Can modify the other two events

3, on the release events, custom events, custom events and more detailed information, please refer to ". Net framework for program design," Reading Notes Chapter XI _ events


.Net Database Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.