C# Events Example Part 3


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

This post follows from our previous post called C# Events Example Part 2. In this post we will make another change to our code.

In our example we are processing Items. These could be anything. Perhaps we are processing text files or some type of media file. Perhaps the item is a Customer.

What if we wanted each of the subscribers to know something about the Item that we are processing? How do we do that? First, have a look at the code for the ItemProcessor.cs file. We will change EventArgs. We can create another class that inherits from EventArgs and adds a property. Here below is the code we have now.

using System;
using System.Threading;
namespace EventsExample
{
    public class ItemProcessor
    {
        // 1. define a delegate (define signature)
        // 2. define an event based on that delegate (ItemProcessed in this case)
        // 3. raise the event
        public delegate void ItemProcessedEventHandler(object source, EventArgs args);
        public event ItemProcessedEventHandler ItemProcessed;

        public void ProcessItem(Item item)
        {
            Console.WriteLine("Processing Item...");
            Thread.Sleep(1500); // delay 1.5 seconds

            OnItemProcessed();
        }
        protected virtual void OnItemProcessed()
        {
            if (ItemProcessed != null)
                ItemProcessed(this, EventArgs.Empty);
        }
    }
}

Above is our code from the previous post. Below is our new code in the ItemProcessor.cs file.

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)
        // 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 ItemProcessedEventHandler 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 });
        }
    }
}

Next we change a little bit of code in our two subscribers. Below is the code from our changed SubscriberOne.cs file. Instead of EventArgs we now use ItemEventArgs and args instead of e. We also have the ability to get the name of the item, as you can see in the Console.WriteLine() line of code.

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

Series Navigation<< C# Events Example Part 2C# Events Example Part 4 >>