public ApplicationStateStack() { m_StatesStack.OnApplicationStateChanged += () => { OnApplicationStateChanged?.Invoke(); }; }
private void OnCurrentProcessExited(object sender, EventArgs e) { OnApplicationStateChanged?.Invoke(this, new ApplicationStateEventArgs(ApplicationState.Terminated)); }
public bool TryStartApplication(string applicationPath, out string errorText) { errorText = string.Empty; try { if (CurrentProcess != null && !CurrentProcess.HasExited) { CurrentProcess.Kill(); } var startInfo = CreateProcessInfo(applicationPath); CurrentProcess = new Process { StartInfo = startInfo, EnableRaisingEvents = true }; CurrentProcess.Exited += OnCurrentProcessExited; CurrentProcess.Start(); CurrentProcess.WaitForInputIdle(30000); if (CurrentProcess.HasExited) { Process childProcess; // Process terminated before idle state. The reason can be start of another process. if (!TryGetChildProcess(CurrentProcess.Id, out childProcess, out errorText)) { return(false); } CurrentProcess = childProcess; } if (CurrentProcess.MainWindowHandle == IntPtr.Zero) { double timer = _mainWindowShowTimeout * 1000; const double CheckDelay = 0.100; do { Thread.Sleep(TimeSpan.FromSeconds(CheckDelay)); timer -= CheckDelay; } while (timer > 0 && CurrentProcess.MainWindowHandle == IntPtr.Zero); if (CurrentProcess.MainWindowHandle == IntPtr.Zero) { errorText = $"{nameof(Process.MainWindowHandle)} not founded"; CurrentProcess.Kill(); return(false); } } AttachToProcess(CurrentProcess); OnApplicationStateChanged?.Invoke(this, new ApplicationStateEventArgs(ApplicationState.Started)); return(true); } catch (Exception exception) { errorText = exception.Message; } return(false); }