|
YOUR FEEDBACK
|
TOP MICROSOFT .NET LINKS Windows Forms Controls Catching SENS Events in .NET
Keep tabs on Windows with System Notification Events
By: Richard Arthur
Jun. 29, 2005 04:00 PM
To register for the SensLogon events, we need to implement the ISensLogon interface, with its required methods. In .NET we usually use delegates and events for notification. To translate between the COM method calls and the .NET events we would rather have, we need a structure to mask the old COM code. When another programmer imports this library, we only want my .NET-friendly classes and events to be exposed. The main class we will expose is called SensLogon, and will have static events for logon, screen lock, etc. To directly receive the messages and then raise the events, this class would need to implement the ISensLogon interface, but that would expose the methods of the interface to anyone, allowing other code to fake the events. To prevent this, we will create a private inner class called SensLogonInterop to implement and hide the interface, register for the events, and forward the events to the containing class. Some of the code for this implementation is in Listing 4 (though much of the code is removed for brevity, it is very similar to what is shown). The SensLogonInterop class has a few constant and static values at the top for registering with the COM+ Event System. These members are used in the class constructor to register it with the Event System. Since it is a private inner class, it can only be instantiated by the SensLogon class. The SensLogon class keeps a static reference to the only instance, thus preventing it from being garbage collected. Once we have a class that will get the events raised by the SENS Events system, we need to add events to the SensLogon class so that it can raise them. This is the trickiest part. COM interoperability is slow, so if no classes within an application are registered to events, we do not want COM to send them. For efficiency, we need a system that registers with COM when a class first registers with SensLogon and removes the COM registration when no classes are left. The typical way of declaring an event in a C# class is inadequate because we cannot perform any special processing when a delegate is attached to an event. Thankfully, C# provides a way of creating custom event registration code: write an event as though you are writing a property, but use the add and remove keywords instead of get and set. The add keyword is for when a delegate is attached to the event, and the remove keyword is for when a delegate is detached from the event. This is illustrated at the bottom of Listing 5. It takes more work to attach and detach events, but because of the RegisterEvent and UnregisterEvent routines, it is a lot simpler than it could have been. Also, to actually raise the events, we have protected static OnEventName methods, which are used to raise the events defined (having a protected method that raises an event is standard procedure for all events in the .NET Framework). The appropriate method is called by the SensLogonInterop class when an event comes in, thereby causing the associated event to be raised, if anyone is registered for that event.
Bringing It All Together
SENS Events in .NET 2.0 MICROSOFT .NET LATEST STORIES
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING NEWS FROM THE WIRES
|
|||||||||||||||||||||||||||||||||||