Events

In this article we will try to understand what is event and how to handle and raise Events.

Note: Before start reading this article please hold a clear concept on delegates. Follow the link to read the previous article on Delegates.

What is Event?

  1. Event is a way to communicate in between of objects, which send notification to its subscribers (who register for that Event).
  2. Events are declared with an associated delegate.
  3. We know previously that delegate hold a reference to a method.
  4. Events sender send notification that’s how event raised and receiver receive the notifications.
  5. We can extends application by adding new methods without changing existing code.
  6. To build loosely coupled applications.
  7. The class containing the event is used to publish the event, called Publisher/Sender.
  8. Containing information’s about the event known as event arguments.
  9. The class accepts this event is called the Subscriber/Receiver.
  10. The method that execute by publisher call is known as event handlers

Diagram of Event Sender and Receiver:
event

Let’s demonstrate with a Sample Example:

namespace Events
{
    class Program
    {
        static void Main(string[] args)
        {
            IQTest objTest = new IQTest(); //Publisher
            NotificationService objNotify = new NotificationService(); //Subscriber

            // += Register a Handler for that Event
            objTest.ExamStarted += objNotify.OnExamStarted;

            objTest.myExam();
            Console.Read();
        }
    }

    //Publisher or Sender
    class IQTest
    {
        //Delegate
        public delegate void ExamEventHandler(object source, EventArgs args);

        //Event
        public event ExamEventHandler ExamStarted;

        public void myExam()
        {
            Console.WriteLine("Exam is Starting..");
            Thread.Sleep(2000);
            OnExamStarted();
        }

        //This method will notify the subscriber 
        protected virtual void OnExamStarted()
        {
            // Invoke the event
            if (ExamStarted != null)
            {
                ExamStarted(this, EventArgs.Empty);
            }
        }
    }

    //Subscriber or Receiver
    class NotificationService
    {
        //Handler : That Match Delegate Signature
        public void OnExamStarted(object source, EventArgs args)
        {
            Console.WriteLine("Your time starts now | start answering..");
        }
    }
}

OutPut:
Exam is Starting..
Your Time starts now | start answering..

Let’s consider the code step by step:
In this code the publisher is IQTest class, there is a method named OnExamStarted will notifies the attended exam students that’s the Exam is started.

Steps of an Event Sender:

  1. Define a Delegates
  2. Define an event based on that Delegates.
  3. Raise/Publish the event

Point 1: Define a Delegates
As we can see here to define a delegate is similar as method declaration as we seen in previous article on Delegates.

//Delegate
public delegate void ExamEventHandler(object source, EventArgs args);

As we know delegate points a method. Here delegate respond between Publisher/Sender and Subscriber/Receiver. Establish the event handler method.

Point 2: Define an event
An event is declared based on the associated Delegates.

//Event
public event ExamEventHandler ExamStarted;

Button click, Text change of a textbox are known as event. Those are generally seen in windows form application.

Point 3: Publish the event
To publish an event we needs to check that event if null or not. This method is responsible for notifying the all subscriber.

protected virtual void OnExamStarted()
{
    // Invoke the event
    if (ExamStarted != null)
    {
        ExamStarted(this, EventArgs.Empty);
    }
}

Here we can see that there are two parameters passed in ExamStarted method, the first parameter (this) which is for the source of the event (current objects) and the second one is to send additional data with that event (this time it’s empty).

Register a Handler for that Event:
This is done with the += operators. We can also have multiple functions registering for the same event, all registered functions are called one by one.

objTest.ExamStarted += objNotify.OnExamStarted;

If we want to remove any function, the -= operator is used.

objTest.ExamStarted -= objNotify.OnExamStarted;

Example of Sending Event Arguments:
Let’s modify the previous Example, this time we will add a new class called ExamEventArgs which has a property named examTime.

The class ExamEventArgs is inheriting from EventArgs.

public class ExamEventArgs : EventArgs
{
    public int examTime { get; set; }
}

And instead of Empty arguments

ExamStarted(this, EventArgs.Empty);

We are using a new instance of ExamEventArgs with initializing time to 10.

ExamStarted(this, new ExamEventArgs

ExamStarted(this, new ExamEventArgs
{
    examTime = 10
});

Finally Merged sample Example:

namespace Events
{
    public class ExamEventArgs : EventArgs
    {
        public int examTime { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            IQTest objTest = new IQTest(); //Publisher
            NotificationService objNotify = new NotificationService(); //Subscriber

            // += Register a Handler for that Event
            objTest.ExamStarted += objNotify.OnExamStarted;

            objTest.myExam();
            Console.Read();
        }
    }

    //Publisher or Sender
    class IQTest
    {
        //Delegate
        public delegate void ExamEventHandler(object source, ExamEventArgs args);

        //Event
        public event ExamEventHandler ExamStarted;

        public void myExam()
        {
            Console.WriteLine("Exam is Starting..");
            Thread.Sleep(2000);
            OnExamStarted();
        }

        //This method will notify the subscriber 
        protected virtual void OnExamStarted()
        {
            // Invoke the event
            if (ExamStarted != null)
            {
                ExamStarted(this, new ExamEventArgs
                {
                    examTime = 10
                });
            }
        }
    }

    //Subscriber or Receiver
    class NotificationService
    {
        //Handler : That Match Delegate Signature
        public void OnExamStarted(object source, ExamEventArgs e)
        {
            Console.WriteLine("Your Time starts now | start answering..");
            Console.WriteLine("Exam Start Time: " + e.examTime);
        }
    }
}

OutPut:
Exam is Starting..
Your Time starts now | start answering..
Exam Start Time: 10

Types of Event Handler:

The Event Handler has two forms, they are:

  1. Normal Form. – EventHandler
  2. Generic Form. – EventHandler

Our Previous example was with normal form of EventHandler.

Instead of creating our own we can use this Generic form. Let’s modify the previous Example instead of using our own custom delegate.

//Delegate
public delegate void ExamEventHandler(object source, ExamEventArgs args);

//Event
public event ExamEventHandler ExamStarted;

We can use a single line of code, and the output will be the same as previous.

//EventHandler
public event EventHandler ExamStarted;

Finally sample Example:

namespace Events
{
    public class ExamEventArgs : EventArgs
    {
        public int examTime { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            IQTest objTest = new IQTest(); //Publisher
            NotificationService objNotify = new NotificationService(); //Subscriber

            // += Register a Handler for that Event
            objTest.ExamStarted += objNotify.OnExamStarted;

            objTest.myExam();
            Console.Read();
        }
    }

    //Publisher or Sender
    class IQTest
    {
        //EventHandler
        public event EventHandler ExamStarted;
        public void myExam()
        {
            Console.WriteLine("Exam is Starting..");
            Thread.Sleep(2000);
            OnExamStarted();
        }

        //This method will notify the subscriber 
        protected virtual void OnExamStarted()
        {
            // Invoke the event
            if (ExamStarted != null)
            {
                ExamStarted(this, new ExamEventArgs
                {
                    examTime = 10
                });
            }
        }
    }

    //Subscriber or Receiver
    class NotificationService
    {
        //Handler : That Match Delegate Signature
        public void OnExamStarted(object source, ExamEventArgs e)
        {
            Console.WriteLine("Your Time starts now | start answering..");
            Console.WriteLine("Exam Start Time: " + e.examTime);
        }
    }
}

OutPut:
Exam is Starting..
Your Time starts now | start answering..
Exam Start Time: 10

Overview:

  1. Events are implemented with delegates.
  2. Events communicate in between of objects.
  3. A method that handles an event is called an event handler.
  4. To build loosely coupled applications events are helpful.
  5. We can extends application by adding new methods without changing other parts of the code.

Author:

Since March 2011, have 8+ years of professional experience on software development, currently working as Senior Software Engineer at s3 Innovate Pte Ltd.

One thought on “Events”

Leave a Reply