Imitation event (Custom Event)

Source: Internet
Author: User

Yesterday I read professional C #2005. net 3.O, and it implements a custom event. If the current time is less than 30 seconds, the event is triggered. Otherwise, the event is canceled. However, in this example, the button event processing is added, and a form application is used.ProgramI personally feel that his event handling is easy to confuse. At the beginning, I thought he had reversed the event handler and the sender. Later, I thought about it and understood it completely. That is, he thinks that the current event is sent by pressing the button. So he first added an actioncanceleventargs event processing and sending link class, and then called the onaction function to send the event. After the onaction (Action) function is called, because it is a delegate and static, and the delegate adds a corresponding processing function in the busentity class. Therefore, this function is directly transferred to form1_action for execution, so that the event processing process is complete. However, this example involves processing other events and adding other elements, so it seems a bit confusing. Although programming will be more confusing in the future, it will be hard for beginners to understand, as I did not fully understand it when I first saw it. Then I wrote a Custom Event Process in my console, which is clear and contains fewer impurities. I hope to help you. Of course, I am a beginner. Please correct the problem. FirstCodeAnalyze it again.

 

Code


UsingSystem;
UsingSystem. Collections. Generic;
UsingSystem. text;
UsingSystem. componentmodel;

Namespace Eventcreate
{
Class Test
{
Static   Public   Void Main ()
{
Dosomething dosomthing =   New Dosomething ();
Dosomthing. Run ();
}
}

Public Delegate VoidMyeventargshandler (ObjectSender, myeventargs ARGs );

Class Myeventargs: canceleventargs
{
Private   String Message = String. empty;
Public   String Message
{
Get { Return Message ;}
Set {Message = Value ;}
}
Public Myeventargs (): Base ()
{
}
Public Myeventargs ( Bool Cancel)
: Base (Cancel)
{
}
Public Myeventargs ( Bool Cancel, String Message)
: Base (Cancel)
{
This . Message = Message;
}
}

Class Controltimer
{
Public   Event Myeventargshandler secondodd;
Public   Void Onsecondodd ( Object Sender, myeventargs ARGs)
{
If ( Null   ! = Secondodd)
Secondodd (sender, argS );
}
}
Class Dosomething
{
Private Controltimer;
Public Dosomething ()
{
Controltimer =   New Controltimer ();
Controltimer. secondodd + =   New Myeventargshandler (dosomething_oddtimer );
}

Public   Void Run ()
{
Random random =   New Random ();
Myeventargs =   New Myeventargs ();
While ( True )
{
Console. writeline ( " Test the second time " );
Int Time = Random. Next ( 5 );
Console. writeline ( " Sleep time is "   + Time );
System. Threading. thread. Sleep (Time *   1000 );
Controltimer. onsecondodd (controltimer, myeventargs );
If (Console. Readline () =   " Q " )
Break ;
}
}

Private   Void Dosomething_oddtimer ( Object Sender, myeventargs ARGs)
{
Int Second;
Args. Cancel =   ! Issecondodd ( Out Second );
If (ARGs. Cancel)
Args. Message =   " Wasn't the right time, even second! "   + Second +   " S " ;
Else
Args. Message =   " The right time, "   + Second +   " S, odd second " ;
Console. writeline (ARGs. Message );
}

Private   Bool Issecondodd ( Out   Int Second)
{
Datetime now = Datetime. now;
Second = Now. Second;
If (Second %   2   =   1 )
Return   True ;
Return   False ;
}
}
}

 

My dosomething class imitates the processing method of the application class, so it can be seen as a form window executed in the application. It is equivalent to form1. The controltimer class can be regarded as a control, for example, a button. Event processing is executed in the dosomething class. The event sender sends a secondodd event of the controltimer class in the run function because of a sleep time. The event is finally executed in the controltimer class, this class is used for sending and processing in the dosomething class to determine whether the number of seconds in the current time is an odd number.

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.