private void RegisterSelChangedEventAT(object state)
        {
            IUIAutomationElement elem = null;

            try {
                pAutomation.ElementFromHandle(handler.GetHwnd(), out elem);
                if (elem != null)
                {
                    pAutomation.AddAutomationEventHandler(UIA_Selection_InvalidatedEventId,
                                                          elem, TreeScope_Element, IntPtr.Zero, handler);
                    pAutomation.AddAutomationEventHandler(UIA_SelectionItem_ElementAddedToSelectionEventId,
                                                          elem, TreeScope_Descendants, IntPtr.Zero, handler);
                    pAutomation.AddAutomationEventHandler(UIA_SelectionItem_ElementRemovedFromSelectionEventId,
                                                          elem, TreeScope_Descendants, IntPtr.Zero, handler);
                    pAutomation.AddAutomationEventHandler(UIA_SelectionItem_ElementSelectedEventId,
                                                          elem, TreeScope_Descendants, IntPtr.Zero, handler);
                }
            }
            catch (Exception ex) {
            }
            finally {
                if (elem != null)
                {
                    Marshal.ReleaseComObject(elem);
                }
                ((AutoResetEvent)state).Set();
            }
        }
 public override void Init()
 {
     IUIAutomation uia = this.IUIAutomation;
     if (uia != null)
     {
         uia.AddAutomationEventHandler(this.EventId, this.Element, this.Scope, null, this);
         this.IsHooked = true;
     }
 }