一个实现自定义event的文章。。。我还没有完全摸透。。不知道有没人有兴趣。。新手就不用看了,先学会走...
shows the functionality of event handling, a loop is created to count to some number and stop. During this count, a specific value is interrogated within the loop. When the specified value is equal to the loop value, the event is fired. In this case the value is 400000. The loop runs from 1 to 500000. When the value 400000 is reached the OnMessage method is called and the event is raised. Note: The InitEvent() method is called from Page_Load() method when the page is requested. The InitEvent() method is depicted in Listing 2.
Listing 2
private void InitEvent()
{
MessagingHandler mh = new MessagingHandler();
MessageEvent me = new MessageEvent();
me.Message += new MessageHandler(mh.GetMessageData);
for (int i = 1; i <= 500000; i++)
{
if (i == 400000)
{
MessageEventArgs e = new MessageEventArgs();
me.OnMessage(e);
lblMessage.Text = mh.EventMessage;
}
}
}
As you can see this is certainly overkill to display a line of text in a label field on a Web page. However, it is useful to examine the workflow of an event that is captured and handled in the .NET Framework. In order to make sense of the code fragment in Listing 2, we need to examine the classes that comprise the event structure. Below in Listing 3 shows the respective components that will raise and handle the event as it transpires.
Listing 3
public class MessageEventArgs : EventArgs
{
public string MessageText
{
get
{
return ("There has been 400000 values processed.");
}
}
}
public delegate void MessageHandler(object sender, MessageEventArgs e);
public class MessageEvent
{
public event MessageHandler Message;
public void OnMessage(MessageEventArgs e)
{
if (Message != null)
{
Message(this, e);
&nbs
Listing 2
private void InitEvent()
{
MessagingHandler mh = new MessagingHandler();
MessageEvent me = new MessageEvent();
me.Message += new MessageHandler(mh.GetMessageData);
for (int i = 1; i <= 500000; i++)
{
if (i == 400000)
{
MessageEventArgs e = new MessageEventArgs();
me.OnMessage(e);
lblMessage.Text = mh.EventMessage;
}
}
}
As you can see this is certainly overkill to display a line of text in a label field on a Web page. However, it is useful to examine the workflow of an event that is captured and handled in the .NET Framework. In order to make sense of the code fragment in Listing 2, we need to examine the classes that comprise the event structure. Below in Listing 3 shows the respective components that will raise and handle the event as it transpires.
Listing 3
public class MessageEventArgs : EventArgs
{
public string MessageText
{
get
{
return ("There has been 400000 values processed.");
}
}
}
public delegate void MessageHandler(object sender, MessageEventArgs e);
public class MessageEvent
{
public event MessageHandler Message;
public void OnMessage(MessageEventArgs e)
{
if (Message != null)
{
Message(this, e);
&nbs
| 对此文章发表了评论 |

