C# – Notify changes with custom EventHandler

During my development I must find a way to notify about changes in properties of class during threads are running. To invoke event relevant to properties, we can use available EventHandler delegate or create our own one (you know that if we use our own, we always have flexible way to handle event). In this small example below I would like to demonstrate how we can create our own delegate.

This example creates a class PCProducer with 2 properties PCOwner and PCChip and I will use EventHandler and my custom one to catch all notifications created when I change the value of properties.

class PCProducer
{
	private string strPCChip;
	private string strPCOwner;

	public delegate void OwnerChangedEventHandler(string strNewOwner);
	public event OwnerChangedEventHandler OwnerChanged;
	public event EventHandler ChipChanged;

	public string PCOwner
	{
		get { return strPCOwner; }
		set
		{
			strPCOwner = value;
			if (OwnerChanged != null)
			{
				OwnerChanged(value);
			}
		}

	}

	public string PCChip
	{
		get { return strPCChip; }
		set {
			strPCChip = value;
			if (ChipChanged != null) ChipChanged(this, new EventArgs());
		}
	}
}

As you can see in the code snippet above I use both EventHandler and my custom one to release events. EventHandler delegate represents the method that will handle an event that has no event data. The standard signature of an event handler delegate defines a method that does not return a value, whose first parameter is of type Object and refers to the instance that raises the event, and whose second parameter is derived from type EventArgs and holds the event data. If the event does not generate event data, the second parameter is simply an instance of EventArgs. Otherwise, the second parameter is a custom type derived from EventArgs and supplies any fields or properties needed to hold the event data. Meanwhile my delegate takes only one argument and take no parameter as type of Object. We can of course expand the delegate to receive first parameter as type of PCProducer. We are completely free to expand our delegate. We can pass arguments as the way we like and the released event can be caught as following

 private void btnInvoke_Click(object sender, EventArgs e)
{
	Thread thCar = new Thread(StartMyThread);
	thCar.Start();
}

private void StartMyThread()
{
	PCProducer carMercedes = new PCProducer();

	carMercedes.OwnerChanged += new PCProducer.OwnerChangedEventHandler(carMercedes_OwnerChanged);
	carMercedes.PCOwner = "rongchaua";

	carMercedes.ChipChanged += new EventHandler(carMercedes_ChipChanged);
	carMercedes.PCChip = "Intel";
}

void carMercedes_ChipChanged(object sender, EventArgs e)
{
	MessageBox.Show("Chip changed");
}

void carMercedes_OwnerChanged(string strNewOwner)
{
	MessageBox.Show("Owner changed");
}

The complete source code you can download here “Notify changes with custom EventHandler

Update 15.02.2010
We can use custom EventHandler to update our GUI from other class. To do so, we just declare an event in class and handle this event in form and update component in form as following
– Define event in class

class OtherClass
{
	public delegate void UpdateLabelDelegate(string strMessage);
	public event UpdateLabelDelegate UpdateLabel;


	public void MemberFunction()
	{
		UpdateLabel("This is text from other class");
	}
}

– Handle event in form

private void Window_Loaded(object sender, RoutedEventArgs e)
{
	OtherClass ocClass = new OtherClass();
	ocClass.UpdateLabel += new OtherClass.UpdateLabelDelegate(ocClass_UpdateLabel);
	ocClass.MemberFunction();
}

void ocClass_UpdateLabel(string strMessage)
{
	lblMessage.Content = strMessage;
}

The complete source code you can download here “Access GUI from other class”

Leave a Reply

Your email address will not be published. Required fields are marked *