Events in C#

Passing messages from a dll to the 'main' project using delegates

In the dll:

public delegate void WarningEventHandler(string type, string warning);
public static event WarningEventHandler SendWarning;

SendWarning("new", message);

In the main:

dll.SendWarning += new dll.WarningEventHandler(dll_SendWarning);
// Remove handler with: dll.SendWarning -= new dll.WarningEventHandler(dll_SendWarning);

void dll_SendWarning(string type, string warning)
  throw new NotImplementedException();

Version without delegates

public event EventHandler<XbrlFactEventArgs> FactAdded;
public void OnFactAdded(XbrlFact fact)
  EventHandler<XbrlFactEventArgs> temp = FactAdded; // making the call thread safe
  if (temp != null)
    temp(this, new XbrlFactEventArgs(fact));
dll.FactAdded += new EventHandler<XbrlFactEventArgs>(HandleFactAdded);
protected virtual void HandleFactAdded(object sender, XbrlFactEventArgs args)
  Do stuff
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License