/// <summary> /// Handles structure-changed events. If a new app window has been added, this method ensures /// it's in the list of runtime IDs and subscribed to window-close events. /// </summary> /// <param name="sender">Object that raised the event.</param> /// <param name="e">Event arguments.</param> /// <remarks> /// An exception can be thrown by the UI Automation core if the element disappears /// before it can be processed -- for example, if a menu item is only briefly visible. /// This exception cannot be caught here because it crosses native/managed boundaries. /// In the debugger, you can ignore it and continue execution. The exception does not cause /// a break when the executable is being run. /// </remarks> private void OnStructureChanged(object sender, StructureChangedEventArgs e) { AutomationElement element = sender as AutomationElement; //element logger.Info("OnStructureChanged: " + e.StructureChangeType); if (e.StructureChangeType == StructureChangeType.ChildAdded) { try { int[] rid = e.GetRuntimeId(); if (!runtimeIdList.Contains(rid) && !Comparison.CheckListElementContain(listAutoElementAdded, element) && !Comparison.CheckListElementContain(listElementAdded, element)) { newElementAddedNotify.NewElementAddedCallback(element); } runtimeIdList.Add(rid); listAutoElementAdded.Add(element); } catch (ElementNotAvailableException ex) { logger.Error("Element not available exception. " + Utils.ExceptionToString(ex)); } } }