A simple event notifier for winforms

A simple event notifier for winforms

This is a sample snippet which can be used for notification across our winforms or console applications.

Most of the time we’ll need a simple snippet which can do the job of pushing notifications across your applications. All we have to do is to just create a typed or un-typed delegate which will be used for this purpose. Here is a simple step by step snippet which you can use as a ready-made one.

  1. Create a delegate with the type required for the application. This can be wrapped inside a class which is reusable in your whole application.
    public class NotificationManager
    {
    	public delegate void ReceiveAlerts(T alert);
    	public static ReceiveAlerts ReceiveAlertUpdates;
    
    	public static void Notify(MyAlertData pAlert)
    	{
    		if (ReceiveAlertUpdates != null)
    		{
    			ReceiveAlertUpdates(pAlert);
    		}
    	}
    }
  2. Add a variable for the delegate in your client class.
    	NotificationManager.ReceiveAlertUpdates += new NotificationManager.ReceiveAlerts(AlertEventReceived);
    
    	private void AlertEventReceived(MyAlertData alert)
    	{
    		String strdata = alert.ToString();
    		Invoke((MethodInvoker)delegate { txtReceivedEvents.AppendText(strdata); });
    	}
  3. Generate and send the event into the system. It will automatically get captured and can be used for further processing.
  4. As done in the previoius code the GUI controls should use InvokeRequired property and invoke a delegate in case the caller is in a different thread than the control.
  5. This can be can downloaded fromĀ Forms Notifier Sample download. A screenshot of the forms app is as below.

Forms Notifier