/// <summary> /// Unregister all event listeners /// </summary> public void UnregisterAllAutomationEventListners() { var msg = new EventListenerFactoryMessage() { MessageType = EventListenerFactoryMessageType.UnregisterAllEventListeners, }; AddMessageToQueue(msg); msg.WaitForProcessed(2000);// expect to be done in 2 seconds. }
/// <summary> /// Add a new Factory Message /// </summary> /// <param name="msgData"></param> private void AddMessageToQueue(EventListenerFactoryMessage msgData) { // Request the lock, and block until it is obtained. lock (_msgQueue) { // When the lock is obtained, add an element. _msgQueue.Enqueue(msgData); } _autoEventMsg.Set(); }
/// <summary> /// Unregister a automation event listener /// </summary> /// <param name="eventId"></param> /// <param name="wait">wait to complete</param> public void UnregisterAutomationEventListener(int eventId, bool wait = false) { var msg = new EventListenerFactoryMessage() { MessageType = EventListenerFactoryMessageType.UnregisterEventListener, EventId = eventId }; AddMessageToQueue(msg); if (wait) { msg.WaitForProcessed(2000);// expect to be done in 2 seconds. } }
/// <summary> /// Process Register event message /// </summary> /// <param name="msgData"></param> private void RegisterEventListener(EventListenerFactoryMessage msgData) { try { EventMessage m = null; switch (msgData.EventId) { case EventType.UIA_AutomationFocusChangedEventId: if (this.EventListenerFocusChanged == null) { this.EventListenerFocusChanged = new FocusChangedEventListener(this.UIAutomation, msgData.Listener); } break; case EventType.UIA_StructureChangedEventId: if (this.EventListenerStructureChanged == null) { this.EventListenerStructureChanged = new StructureChangedEventListener(this.UIAutomation, this.RootElement.PlatformObject, this.Scope, msgData.Listener); } break; case EventType.UIA_AutomationPropertyChangedEventId: if (this.EventListenerPropertyChanged == null) { this.EventListenerPropertyChanged = new PropertyChangedEventListener(this.UIAutomation, this.RootElement.PlatformObject, this.Scope, msgData.Listener, msgData.Properties); } break; case EventType.UIA_TextEdit_TextChangedEventId: if (this.EventListenerTextEditTextChanged == null) { this.EventListenerTextEditTextChanged = new TextEditTextChangedEventListener(this.UIAutomation8, this.RootElement.PlatformObject, this.Scope, msgData.Listener); } break; case EventType.UIA_ChangesEventId: if (this.EventListenerChanges == null) { this.EventListenerChanges = new ChangesEventListener(this.UIAutomation8, this.RootElement.PlatformObject, this.Scope, msgData.Listener); } break; case EventType.UIA_NotificationEventId: if (NativeMethods.IsWindowsRS3OrLater()) { if (this.EventListenerNotification == null) { this.EventListenerNotification = new NotificationEventListener(this.UIAutomation8, this.RootElement.PlatformObject, this.Scope, msgData.Listener); } } else { m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); m.Properties = new List <KeyValuePair <string, dynamic> >() { new KeyValuePair <string, dynamic>("Message", "Event listener registration is rejected."), new KeyValuePair <string, dynamic>("Event Id", msgData.EventId), new KeyValuePair <string, dynamic>("Event Name", EventType.GetInstance().GetNameById(msgData.EventId)), new KeyValuePair <string, dynamic>("Reason", "Not supported platform"), }; msgData.Listener(m); } break; case EventType.UIA_ActiveTextPositionChangedEventId: if (NativeMethods.IsWindowsRS5OrLater()) { if (this.EventListenerNotification == null) { this.EventListenerActiveTextPositionChanged = new ActiveTextPositionChangedEventListener(this.UIAutomation8, this.RootElement.PlatformObject, this.Scope, msgData.Listener); } } else { m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); m.Properties = new List <KeyValuePair <string, dynamic> >() { new KeyValuePair <string, dynamic>("Message", "Event listener registration is rejected."), new KeyValuePair <string, dynamic>("Event Id", msgData.EventId), new KeyValuePair <string, dynamic>("Event Name", EventType.GetInstance().GetNameById(msgData.EventId)), new KeyValuePair <string, dynamic>("Reason", "Not supported platform"), }; msgData.Listener(m); } break; default: if (this.EventListeners.ContainsKey(msgData.EventId) == false) { this.EventListeners.Add(msgData.EventId, new EventListener(this.UIAutomation, this.RootElement.PlatformObject, this.Scope, msgData.EventId, msgData.Listener)); } break; } m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); m.Properties = new List <KeyValuePair <string, dynamic> >() { new KeyValuePair <string, dynamic>("Message", "Succeeded to register an event listener"), new KeyValuePair <string, dynamic>("Event Id", msgData.EventId), new KeyValuePair <string, dynamic>("Event Name", EventType.GetInstance().GetNameById(msgData.EventId)), }; msgData.Listener(m); if (msgData.EventId == EventType.UIA_AutomationFocusChangedEventId) { this.EventListenerFocusChanged.ReadyToListen = true; } } catch (Exception e) { var m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); m.Properties = new List <KeyValuePair <string, dynamic> >() { new KeyValuePair <string, dynamic>("Message", "Failed to register an event listener"), new KeyValuePair <string, dynamic>("Event Id", msgData.EventId), new KeyValuePair <string, dynamic>("Event Name", EventType.GetInstance().GetNameById(msgData.EventId)), new KeyValuePair <string, dynamic>("Error", e.Message) }; msgData.Listener(m); } }
/// <summary> /// Process Unregister event message /// </summary> /// <param name="msgData"></param> private void UnregisterEventListener(EventListenerFactoryMessage msgData) { HandleUIAutomationEventMessage listener = null; try { switch (msgData.EventId) { case EventType.UIA_AutomationFocusChangedEventId: if (this.EventListenerFocusChanged != null) { listener = this.EventListenerFocusChanged.ListenEventMessage; this.EventListenerFocusChanged.Dispose(); this.EventListenerFocusChanged = null; } break; case EventType.UIA_StructureChangedEventId: if (this.EventListenerStructureChanged != null) { listener = this.EventListenerStructureChanged.ListenEventMessage; this.EventListenerStructureChanged.Dispose(); this.EventListenerStructureChanged = null; } break; case EventType.UIA_AutomationPropertyChangedEventId: if (this.EventListenerPropertyChanged != null) { listener = this.EventListenerPropertyChanged.ListenEventMessage; this.EventListenerPropertyChanged.Dispose(); this.EventListenerPropertyChanged = null; } break; case EventType.UIA_TextEdit_TextChangedEventId: if (this.EventListenerTextEditTextChanged != null) { listener = this.EventListenerTextEditTextChanged.ListenEventMessage; this.EventListenerTextEditTextChanged.Dispose(); this.EventListenerTextEditTextChanged = null; } break; case EventType.UIA_ChangesEventId: if (this.EventListenerChanges != null) { listener = this.EventListenerChanges.ListenEventMessage; this.EventListenerChanges.Dispose(); this.EventListenerChanges = null; } break; case EventType.UIA_NotificationEventId: if (this.EventListenerNotification != null) { listener = this.EventListenerNotification.ListenEventMessage; this.EventListenerNotification.Dispose(); this.EventListenerNotification = null; } break; case EventType.UIA_ActiveTextPositionChangedEventId: if (this.EventListenerActiveTextPositionChanged != null) { listener = this.EventListenerActiveTextPositionChanged.ListenEventMessage; this.EventListenerActiveTextPositionChanged.Dispose(); this.EventListenerActiveTextPositionChanged = null; } break; default: if (this.EventListeners.ContainsKey(msgData.EventId)) { var l = this.EventListeners[msgData.EventId]; listener = l.ListenEventMessage; this.EventListeners.Remove(msgData.EventId); l.Dispose(); } break; } if (listener != null) { var m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); m.Properties = new List <KeyValuePair <string, dynamic> >() { new KeyValuePair <string, dynamic>("Message", "Succeeded to unregister a event listeners"), new KeyValuePair <string, dynamic>("Event Id", msgData.EventId), new KeyValuePair <string, dynamic>("Event Name", EventType.GetInstance().GetNameById(msgData.EventId)), }; listener(m); } } catch (Exception e) { var m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); m.Properties = new List <KeyValuePair <string, dynamic> >() { new KeyValuePair <string, dynamic>("Message", "Failed to unregister a event listeners"), new KeyValuePair <string, dynamic>("Event Id", msgData.EventId), new KeyValuePair <string, dynamic>("Event Name", EventType.GetInstance().GetNameById(msgData.EventId)), new KeyValuePair <string, dynamic>("Error", e.Message) }; listener(m); /// it is very unexpected situation. /// need to figure out a way to prevent it or handle it more gracefully } }