Tips (7): Agent & event, and Agent
Proxy delegate:
Object reference points to a specific type of object.
The proxy points to a specific type of method.
Proxy Step 4:
Class Program
{
// Define the frist proxy
Public delegate void first (int I );
// Main function, main Entry
Static void Main (string [] args)
{
// Create a first-type reference
First MyDelegate = null;
// Create a proxy reference pointing to the show Method
MyDelegate + = new first (show );
// Call the show method through proxy reference
MyDelegate (666 );
Console. ReadKey ();
}
// Show Method
Public static void show (int I)
{
Console. WriteLine (I. ToString ());
}
}
Class Program
{
// Define the frist proxy
Public delegate void first (int I );
// Main function, main Entry
Static void Main (string [] args)
{
// Create a first-type reference
First MyDelegate = null;
// Create a proxy reference pointing to the show Method
MyDelegate + = new first (show );
// Call the show method through proxy reference
Diao( 666, MyDelegate );
Console. ReadKey ();
}
// Show Method
Public static void show (int I)
{
Console. WriteLine (I. ToString ());
}
//
Public static void diao (int I, first dele)
{
Dele (I );
}
}
Event:
// Define the EventDelegate proxy
Public delegate void EventDelegate ();
Class Program
{
// Main function, main Entry
Static void Main (string [] args)
{
// Instantiate ClockTimer
ClockTimer clockTimer = new ClockTimer ();
// Add the OnClockTimer method to MyEvent
ClockTimer. MyEvent + = new EventDelegate (OnClockTimer );
// Execute the show method of the clickTimer object
ClockTimer. show ();
Console. ReadLine ();
}
// Acceptance Method
Public static void OnClockTimer ()
{
Console. WriteLine ("receive clock event ");
}
}
// Event generation class
Public class ClockTimer
{
// Define an event)
Public event EventDelegate MyEvent;
// Event Generation Method
Public void show ()
{
For (int I = 0; I <1000; I ++)
{
// Generate an event
MyEvent ();
// Sleep for 1 second
Thread. Sleep (1000); // System. Threading;
}
}
}
Masters, the above is my understanding of event and delegate. If something is wrong, please help me to point it out. Thank you!