Please post questions to forums.msdn.com; there are tons of people who read and respond to questions every day there.
TO UNREGISTER INTEREST in an event, simply use the -= operator instead of the += operator in the event registration pattern. For example:
public static class LaserCharger
{
public static event EventHandler FireMahLazer;
public static void LaserChargedLol()
{
EventHandler foo = FireMahLazer;
if (foo != null)
foo(null, EventArgs.Empty);
}
}
public class ClassThatConsumesEvent
{
public void RegisterInterest()
{
LaserCharger.FireMahLazer += LaserFired;
}
public void UnregisterInterest()
{
LaserCharger.FireMahLazer -= LaserFired;
}
public void LaserFired(Object sender, EventArgs e)
{
System.Console.WriteLine("Laser has fired!");
}
}
This code describes a simple class that exposes a static event (static so that we don't need an instance of the class in order to fire the event is the only reason; works just as well for instance events) and a method to fire that event. Notice the event firing method LaserChargedLol()'s pattern--we first create a handle on the event, check that it isn't null, and then invoke it (passing a null and empty event args because they don't matter in this example). This is because another thread may null the event object after we check it and before we invoke it. Grabbing a handle on the event object prevents this from happening. Understanding how .NET implements events is required in order to fully understand this; I would strongly suggest you purchase and read CLR Via C# by J. Richter if you have questions.
Now as for ClassThatConsumesEvent... It has three methods: A method to register interest, a method to unregister interest, and an event handler. To register interest, just += the event handler to the event you wish to listen to. You may often see a different version of this as:
LaserCharger.FireMahLazer += new EventHandler(LaserFired);
LaserCharger.FireMahLazer -= new EventHandler(LaserFired);
This is just the longhand version of the code above in RegisterInterest and UnregisterInterest. They both compile to the same byte code.
Don't forget to unregister interest in events; the event object keeps a reference to all objects that have registered event handlers with it. If you do not unregister interest, that reference may keep objects alive for a longer time than expected. In long running applications, this can appear to be a memory leak.
BTW, the Community Content editor SUCKS.
[Response: I'm happy to report that MSDN has significant editor improvements that will be launched later this month. If you have specific feedback or requests, please post them to our connect feedback site: https://connect.microsoft.com/feedback/default.aspx?SiteID=112. Thanks!]