/// <summary> /// Blocks execution of the current thread until the main window of the /// application is displayed or until the specified timeout interval elapses. /// </summary> /// <param name="timeout">The timeout interval.</param> public void WaitForMainWindow(TimeSpan timeout) { TimeSpan zero = TimeSpan.FromMilliseconds(0); TimeSpan delta = TimeSpan.FromMilliseconds(10); while (!this.IsMainWindowOpened && timeout > zero) { if (settings.InProcessApplicationType == InProcessApplicationType.InProcessSameThread) { DispatcherOperations.WaitFor(DispatcherPriority.ApplicationIdle); } else { Thread.Sleep(10); } timeout -= delta; } // set as active if (settings.InProcessApplicationType == InProcessApplicationType.InProcessSameThread) { app.MainWindow.Activate(); } else { this.DispatcherInvoke(() => { app.MainWindow.Activate(); }); } }
/// <summary> /// Closes the application /// </summary> public void Close() { if (app != null) { if (settings.InProcessApplicationType == InProcessApplicationType.InProcessSameThread) { CloseHelper(); DispatcherOperations.WaitFor(DispatcherPriority.ApplicationIdle); app.Shutdown(); app = null; } else { DispatcherInvoke(() => { CloseHelper(); }); DispatcherOperations.WaitFor(DispatcherPriority.ApplicationIdle); app.Dispatcher.InvokeShutdown(); app = null; } } }
/// <summary> /// Waits for the given window to activate. /// </summary> /// <remarks> /// Note that this will not work for dialogs that block the thread. /// </remarks> /// <param name="windowName">The AutomationProperties.AutomationIdProperty value of the window</param> /// <param name="timeout">The timeout interval.</param> public void WaitForWindow(string windowName, TimeSpan timeout) { bool isWindowOpened = false; TimeSpan zero = TimeSpan.FromMilliseconds(0); TimeSpan delta = TimeSpan.FromMilliseconds(10); while (!isWindowOpened && timeout > zero) { DispatcherOperations.WaitFor(TimeSpan.FromMilliseconds(100)); timeout -= delta; if (settings.InProcessApplicationType == InProcessApplicationType.InProcessSameThread) { isWindowOpened = WaitForWindowHelper(windowName); } else { isWindowOpened = DispatcherInvoke <bool>(() => { return(WaitForWindowHelper(windowName)); }); } } }