Information Technology Reference
In-Depth Information
public class
MyTCEventArgs
: EventArgs
{
public string Message;
Declaration of custom class
public MyTCEventArgs(string s) {
Message = s;
}
}
public class MyTimerClass
Event declaration
{
↓
public event
EventHandler<MyTCEventArgs>
Elapsed; // Event declaration
private void OnOneSecond(object obj, EventArgs e)
{
if (Elapsed != null)
{
MyTCEventArgs
mtcea =
new MyTCEventArgs("Message from OnOneSecond");
Code to raise event
Elapsed(obj, mtcea);
}
}
...
}
class ClassA
{
public void TimerHandlerA(object obj,
MyTCEventArgs
e)
{
Console.WriteLine("Class A Message: {0}", e.Message);
Event handler
}
}
class Program
{
static void Main()
{
ClassA ca = new ClassA();
MyTimerClass mc = new MyTimerClass();
Register event handler
mc.Elapsed +=
↓
// Register handler.
new
EventHandler<MyTCEventArgs>
(ca.TimerHandlerA);
Thread.Sleep(3250);
}
}