Ejemplo n.º 1
0
        /// <summary>
        /// Starts a process and blocks until the Main Windows has loaded
        /// returns the MainWindow's AutomationElement as an out parameter
        /// </summary>
        public static Process StartProcess(ProcessStartInfo startInfo, out AutomationElement rootElement)
        {
            AutomationDelegate getRoot = p =>
            {
                return(AutomationElement.FromHandle(p.MainWindowHandle));
            };

            return(StartProcess(startInfo, getRoot, out rootElement));
        }
Ejemplo n.º 2
0
        internal static Process StartProcess(ProcessStartInfo startInfo, AutomationDelegate onProcessWindowOpened, out AutomationElement rootElement)
        {
            Process           process = null;
            AutomationElement element = null;

            if (onProcessWindowOpened != null)
            {
                AutomationEventHandler onWindowOpenedHandler = null;

                onWindowOpenedHandler = (sender, e) =>
                {
                    if (process != null)
                    {
                        if (process.MainWindowHandle != IntPtr.Zero)
                        {
                            Automation.RemoveAutomationEventHandler
                            (
                                WindowPatternIdentifiers.WindowOpenedEvent,
                                AutomationElement.RootElement,
                                onWindowOpenedHandler
                            );

                            element = onProcessWindowOpened(process);
                        }
                        else
                        {
                            process.Refresh();
                        }
                    }
                };

                Automation.AddAutomationEventHandler
                (
                    WindowPatternIdentifiers.WindowOpenedEvent,
                    AutomationElement.RootElement,
                    TreeScope.Subtree,
                    onWindowOpenedHandler
                );
                process = Process.Start(startInfo);
                while (element == null)
                {
                    Thread.Sleep(10);
                }
            }
            rootElement = element;
            return(process);
        }