Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

WindowClosedEventArgs.GetRuntimeId Method

Retrieves the UI Automation runtime identifier (ID) associated with this event.

Namespace:  System.Windows.Automation
Assembly:  UIAutomationTypes (in UIAutomationTypes.dll)
public int[] GetRuntimeId()

Return Value

Type: System.Int32[]
The UI Automation runtime ID of the window on which the event was raised.

When your application receives a window-closed event, the sender parameter of the event handler cannot be used to obtain information about the window that has closed, because the corresponding Microsoft UI Automation element is no longer valid. Use GetRuntimeId to match the window with a known identifier.

In the following example, the handler for the window-closed event ascertains whether the runtime ID of the closed window is in a list of the IDs of open windows. If the runtime ID is present, it is removed from the list.

        /// <summary> 
        /// Handles window-closed events. Removes the window from the top-level window list. 
        /// </summary> 
        /// <param name="sender">Object that raised the event.</param>
        /// <param name="e">Event arguments.</param>
        /// <remarks> 
        /// runtimteIds is an ArrayList that contains the runtime IDs of all top-level windows. 
        /// </remarks> 
        private void WindowClosedHandler(object sender, AutomationEventArgs e)
        {
            WindowClosedEventArgs windowEventArgs = (WindowClosedEventArgs)e;
            int[] runtimeIdentifiers = windowEventArgs.GetRuntimeId();
            int index = RuntimeIdListed(runtimeIdentifiers, savedRuntimeIds);
            if (index >= 0)
            {
                savedRuntimeIds.RemoveAt(index);
                Console.WriteLine("Window closed.");
            }
        }

        /// <summary> 
        /// Ascertains whether the window is in the list. 
        /// </summary> 
        /// <param name="rid">Runtime ID of the window.</param>
        /// <returns>Index of the ID in the list, or -1 if it is not listed.</returns> 
        /// <remarks> 
        /// runtimteIds is an ArrayList that contains the runtime IDs of all top-level windows. 
        /// </remarks> 
        private int RuntimeIdListed(int[] runtimeId, ArrayList runtimeIds)
        {
            for (int x = 0; x < runtimeIds.Count; x++)
            {
                int[] listedId = (int[])runtimeIds[x];
                if (Automation.Compare(listedId, runtimeId))
                {
                    return x;
                }
            }
            return -1;
        }

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.