/// <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) { #pragma warning disable CA2000 // Call IDisposable.Dispose() var m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); #pragma warning restore CA2000 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); } } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception e) { e.ReportException(); #pragma warning disable CA2000 // Call IDisposable.Dispose() var m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); #pragma warning restore CA2000 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 } #pragma warning restore CA1031 // Do not catch general exception types }
/// <summary> /// Process Register event message /// </summary> /// <param name="msgData"></param> private void RegisterEventListener(EventListenerFactoryMessage msgData) { try { EventMessage m = null; var win32Helper = new Win32Helper(); 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 (win32Helper.IsWindowsRS3OrLater()) { if (this.EventListenerNotification == null) { this.EventListenerNotification = new NotificationEventListener(this.UIAutomation8, this.RootElement.PlatformObject, this.Scope, msgData.Listener); } } else { #pragma warning disable CA2000 // Call IDisposable.Dispose() m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); #pragma warning restore CA2000 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 (win32Helper.IsWindowsRS5OrLater()) { if (this.EventListenerNotification == null) { this.EventListenerActiveTextPositionChanged = new ActiveTextPositionChangedEventListener(this.UIAutomation8, this.RootElement.PlatformObject, this.Scope, msgData.Listener); } } else { #pragma warning disable CA2000 // Call IDisposable.Dispose() m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); #pragma warning restore CA2000 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; } #pragma warning disable CA2000 // Call IDisposable.Dispose() m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); #pragma warning restore CA2000 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; } } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception e) { e.ReportException(); #pragma warning disable CA2000 // Call IDisposable.Dispose() var m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); #pragma warning restore CA2000 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); } #pragma warning restore CA1031 // Do not catch general exception types }
private void UnregisterAllEventListener() { #pragma warning disable CA2000 // Call IDisposeable.Dispose() /// Need to find out a way to handle UnregisterEventListener(new EventListenerFactoryMessage { EventId = EventType.UIA_AutomationFocusChangedEventId }); UnregisterEventListener(new EventListenerFactoryMessage { EventId = EventType.UIA_StructureChangedEventId }); UnregisterEventListener(new EventListenerFactoryMessage { EventId = EventType.UIA_AutomationPropertyChangedEventId }); UnregisterEventListener(new EventListenerFactoryMessage { EventId = EventType.UIA_NotificationEventId }); UnregisterEventListener(new EventListenerFactoryMessage { EventId = EventType.UIA_TextEdit_TextChangedEventId }); UnregisterEventListener(new EventListenerFactoryMessage { EventId = EventType.UIA_ActiveTextPositionChangedEventId }); UnregisterEventListener(new EventListenerFactoryMessage { EventId = EventType.UIA_ChangesEventId }); #pragma warning restore CA2000 HandleUIAutomationEventMessage listener = null; try { foreach (var e in this.EventListeners.Values) { listener = e.ListenEventMessage; e.Dispose(); } this.EventListeners.Clear(); if (listener != null) { #pragma warning disable CA2000 // Call IDisposable.Dispose() var m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); #pragma warning restore CA2000 m.Properties = new List <KeyValuePair <string, dynamic> >() { new KeyValuePair <string, dynamic>("Message", "Succeeded to unregister all event listeners.") }; listener(m); } } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception e) { e.ReportException(); #pragma warning disable CA2000 // Call IDisposable.Dispose() var m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null); #pragma warning restore CA2000 m.Properties = new List <KeyValuePair <string, dynamic> >() { new KeyValuePair <string, dynamic>("Message", $"Failed to unregister all listeners: {e.Message}") }; listener(m); } #pragma warning restore CA1031 // Do not catch general exception types }