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.
- 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); } } }
- 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); }); }
- Generate and send the event into the system. It will automatically get captured and can be used for further processing.
- 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.
- This can be can downloaded from Forms Notifier Sample download. A screenshot of the forms app is as below.