// Helper function to launch an executable file. // Returns true if the executable is launched successfully. private static bool LaunchExe(string ExeName, string Params, string StartDirectory, bool WaitForExit = true, bool HideWindow = true) { bool ret = true; ProcessStartInfo si = new ProcessStartInfo(ExeName, Params); si.WorkingDirectory = StartDirectory; si.WindowStyle = (HideWindow) ? ProcessWindowStyle.Hidden : ProcessWindowStyle.Normal; try { Process p = Process.Start(si); if (WaitForExit) { p.WaitForExit(); } } catch (Exception ex) { OutputText.AppendText(OutputText.OutputStream.Both, String.Format("\r\nError!!! An exception occurred when launching '{0}'. The exception message was '{1}'.\r\n", ExeName, ex.Message)); ret = false; } return(ret); }
/// <summary> /// Starts the server and stores the ProcessID of the command window that hosts it /// </summary> /// <param name="BranchLocation">The path to the openPetra branch for which the server is to be started</param> /// <param name="StartMinimized">Set to True if the server window is to be minimized at startup</param> /// <returns>True if nant.bat was launched successfully. Check the log file to see if the command actually succeeded.</returns> public static bool StartServer(string BranchLocation, bool StartMinimized) { _serverProcessID = 0; bool bRet = LaunchExe("nant.bat", "startPetraServer -logfile:opda.txt", BranchLocation); if (bRet) { // Just wait for things to settle System.Threading.Thread.Sleep(2000); // Call IsServerRunning - this will work out the value for _serverProcessID IsServerRunning(); if (_serverProcessID == 0) { OutputText.AppendText(OutputText.OutputStream.Both, "\r\nThe startServer task was executed but the Assistant failed to figure out the cmd window ID\r\n"); } else if (StartMinimized) { PostMessage(new HandleRef(null, Process.GetProcessById(_serverProcessID).MainWindowHandle), WM_SYSCOMMAND, new IntPtr( SC_MINIMIZE), new IntPtr(0)); } } return(bRet); }
/// <summary> /// Stops the server process and closes the command window that hosted it. (In exceptional circumstances it may not be possible to close the DOS window) /// </summary> /// <param name="BranchLocation">The path to the openPetra branch for which the server is to be stopped</param> /// <returns>True if nant.bat was launched successfully. Check the log file to see if the command actually succeeded.</returns> public static bool StopServer(string BranchLocation) { // Launch the stop task bool bRet = LaunchExe("nant.bat", "stopPetraServer -logfile:opda.txt", BranchLocation); if (bRet && _serverProcessIdIsCmdWindow) { // That command window will have closed but we will be left with the cmd window that hosted the server // We find all cmd windows that are running: if we know the ProcessID that we started we can just kill that one // Otherwise, if there is only one running now we can close it // Failing that we do not know which command window to close, so we have to leave it Process[] curProcesses = Process.GetProcessesByName("cmd"); bool bFound = false; if (_serverProcessID > 0) { foreach (Process p in curProcesses) { if (p.Id == _serverProcessID) { p.Kill(); bFound = true; } } } else if (curProcesses.Length == 1) { // There is only one cmd window, so we can close it anyway curProcesses[0].Kill(); bFound = true; } if (!bFound) { OutputText.AppendText(OutputText.OutputStream.Both, String.Format("\r\nFailed to find ProcessID {0} so cmd window was not closed\r\n", _serverProcessID)); } } _serverProcessID = 0; return(bRet); }