C# Events Example Part 4


This entry is part 5 of 5 in the series C# Events

This is the last post in our example series of events. Here we are going to make one small change to our code and then show all of the code here in this post. You can follow this pattern to add more subscribers as needed. Also, instead of running the code in made you might be running it in another class and have that class be called from Main() to achieve the same result.

The change here in this post achieves the same thing as before with a little less code. We are going to make a change to the code in our publisher: ItemProcessor. Instead of crating our own delegate we can use the one provided by C# .NET framework 2 (or perhaps it was introduced into .NET Framework 3. This delegate comes in two forms. If you type EventHandler you can see the suitcase icon and see that it comes in a regular form and in a generic form. Below is our new line of code. The line it replaced is commented out.

// public delegate void ItemProcessedEventHandler(object source, ItemEventArgs args);
public event EventHandler<ItemEventArgs> ItemProcessed;

If we didn’t need to send any data we could just use the normal form as shown below.

public event Eventhandler VideoEncoded;

Complete Solution

Here below is the code for the complete solution and below that is the screenshot of the results. The first listing is of the Program class.

using System;
namespace EventsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            var item = new Item() { Name = "Item 1 name" };
            var itemProcessor = new ItemProcessor();  // publisher
            var subscriberOne = new SubscriberOne();  // subscriber
            var subscriberTwo = new SubscriberTwo();  // subscriber

            Console.WriteLine("Beginning program EventsExample...");
            
            // itemProcessed is a list of pointers to methods
            itemProcessor.ItemProcessed += subscriberOne.OnItemProcessed;
            itemProcessor.ItemProcessed += subscriberTwo.OnItemProcessed;

            itemProcessor.ProcessItem(item);
        }
    }
}

Out Item.

namespace EventsExample
{
    public class Item
    {
        public string Name { get; set; }  // a property
    }
}

Our publisher which is in the file called ItemProcessor.cs.

using System;
using System.Threading;
namespace EventsExample
{
    public class ItemEventArgs : EventArgs
    {
        public Item Item { get; set; }
    }
    public class ItemProcessor
    {
        // 1. define a delegate (define signature) or use EventHandler<>
        // 2. define an event based on that delegate (ItemProcessed in this case)
        // 3. raise the event

        // public delegate void ItemProcessedEventHandler(object source, ItemEventArgs args);
        public event EventHandler<ItemEventArgs> ItemProcessed;
        
        public void ProcessItem(Item item)
        {
            Console.WriteLine("Processing Item...");
            Thread.Sleep(1500); // delay 1.5 seconds

            OnItemProcessed(item);
        }
        protected virtual void OnItemProcessed(Item item)
        {
            if (ItemProcessed != null)
                ItemProcessed(this, new ItemEventArgs() { Item = item });
        }
    }
}

SubscriberOne.

using System;
namespace EventsExample
{
    public class SubscriberOne
    {
        public void OnItemProcessed(object source, ItemEventArgs args)
        {
            Console.WriteLine("SubscriberOne: " + args.Item.Name);  // maybe send an email
        }
    }
}

SubscriberTwo.

using System;
namespace EventsExample
{
    class SubscriberTwo
    {
        public void OnItemProcessed(object source, ItemEventArgs args)
        {
            Console.WriteLine("SubscriberTwo: " + args.Item.Name);  // maybe send SMS
        }
    }
}

Below is the results of our console program, which are the same as in our previous post.

Series Navigation<< C# Events Example Part 3