Event system explained in detail
using System;
namespace Demo
{
//step 0. use event to simply complex interaction, independent data process module
//step 1. declare a event signatrue
public delegate void OnUserDeviceInsertedEvent(object sender, UserDeviceInsertedArgs e);
//step 2. declare the event args
public class UserDeviceInsertedArgs : EventArgs
{
public string DeviceData;
}
//step 3. declare a real work class which has 1 event field member and 1 working method member, in the method event will be triggered when condiation ready
public class MagCard
{
public OnUserDeviceInsertedEvent OnDeviceInserted;
public void StartingWork()
{
UserDeviceInsertedArgs data = new UserDeviceInsertedArgs();
data.DeviceData = "Demo Data";
if (OnDeviceInserted != null)
{
OnDeviceInserted(this, data);
}
}
}
//step 4. in main system, ord the event and supperly suitable process
static class Program
{
/// <summary>
/// ??????????
/// </summary>
[STAThread]
static void Main()
{
MagCard machine = new MagCard();
machine.OnDeviceInserted += new OnUserDeviceInsertedEvent(EventHandler_OnDeviceInserted);
machine.StartingWork();
Console.ReadKey();
}
private static void EventHandler_OnDeviceInserted(object sender, UserDeviceInsertedArgs e)
{
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine(e.DeviceData);
Console.ResetColor();
}
}
//Coding by MoonKite,2010-June,Study C#
//Thanks for Java2s,I learned a lot form this website.
}
Related examples in the same category