/// <summary> /// 启动一个EXE /// </summary> /// <param name="aProcess">进程句柄</param> /// <param name="sExePath">EXE地址</param> /// <param name="sArg">Main参</param> /// <param name="sWorkDir">工作目录</param> /// <param name="pFunc">输出重定向的响应</param> /// <returns>错误码</returns> public static int StartExe2(ref Process aProcess, string sExePath, string sArg, string sWorkDir = null, System.Diagnostics.DataReceivedEventHandler pFunc = null) { aProcess.StartInfo.FileName = sExePath; aProcess.StartInfo.Arguments = sArg; aProcess.StartInfo.UseShellExecute = false; aProcess.StartInfo.CreateNoWindow = true; //工作目录 if (!String.IsNullOrWhiteSpace(sWorkDir)) { aProcess.StartInfo.WorkingDirectory = sWorkDir; } //输出重定向 if (pFunc != null) { aProcess.StartInfo.RedirectStandardOutput = true; aProcess.OutputDataReceived += pFunc; } try { aProcess.Start(); aProcess.BeginOutputReadLine(); aProcess.WaitForExit(); return(aProcess.ExitCode); } catch { return(-1); } }
public bool InitEngine(String enginePath, String engineIniPath, System.Diagnostics.DataReceivedEventHandler outputDataReceivedProc) { LogHelper.logger.Info("InitEngine called with enginePath: " + enginePath + " and engineIniPath: " + engineIniPath); try { // create process //if (UCI_Engine == null) //{ UCI_Engine = new Process(); UCI_Engine.StartInfo.FileName = enginePath; //UCI_Engine.StartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(enginePath); UCI_Engine.StartInfo.UseShellExecute = false; UCI_Engine.StartInfo.CreateNoWindow = true; UCI_Engine.StartInfo.RedirectStandardInput = true; UCI_Engine.StartInfo.RedirectStandardOutput = true; UCI_Engine.Start(); UCI_Engine.OutputDataReceived += outputDataReceivedProc; UCI_Engine.BeginOutputReadLine(); //} // start new game EngineCommand(kSetUCIMode); ResetEngine(); } catch (Exception exception) { LogHelper.logger.Error(exception.Message); MessageBox.Show( "Engine initialization failed. Please restart. If problem persists, contact your vendor.", "Chessbot", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } LogHelper.logger.Info("InitEngine finished."); return(true); }
/// <summary> /// 启动一个EXE /// </summary> /// <param name="sExePath">EXE地址</param> /// <param name="sArg">Main参</param> /// <param name="sWorkDir">工作目录</param> /// <param name="pFunc">输出重定向的响应</param> /// <returns>错误码</returns> public static int StartExe2(string sExePath, string sArg, string sWorkDir = null, System.Diagnostics.DataReceivedEventHandler pFunc = null) { Process aProcess = new Process(); return(StartExe2(ref aProcess, sExePath, sArg, sWorkDir, pFunc)); }
public Maze GetMove(Maze maze, String outputFilePath, StreamWriter logFile) { var playerOutputFilePath = _workingPath + System.IO.Path.DirectorySeparatorChar + Properties.Settings.Default.SettingBotOutputFileName; File.Delete(playerOutputFilePath); var processName = _workingPath + System.IO.Path.DirectorySeparatorChar + _executableFileName; var arguments = "\"" + outputFilePath + "\""; if (Environment.OSVersion.Platform == PlatformID.Unix) { arguments = processName + " " + arguments; processName = "/bin/bash"; } var p = new Process { StartInfo = { WorkingDirectory = _workingPath, FileName = processName, Arguments = arguments, CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true } }; System.Diagnostics.DataReceivedEventHandler h = (sender, args) => { if (!String.IsNullOrEmpty(args.Data)) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(_workingPath + System.IO.Path.DirectorySeparatorChar + "botlogs_capture.txt", true)) { file.WriteLine(args.Data); } } }; p.OutputDataReceived += h; p.ErrorDataReceived += h; p.Start(); p.BeginOutputReadLine(); p.BeginErrorReadLine(); bool didExit = p.WaitForExit(Properties.Settings.Default.SettingBotOutputTimeoutSeconds * 1000); if (!didExit) { p.Kill(); logFile.WriteLine("[GAME] : Killed process " + processName); } if (p.ExitCode != 0) { logFile.WriteLine("[GAME] : Process exited " + p.ExitCode + " from player " + _playerName); } if (!File.Exists(playerOutputFilePath)) { logFile.WriteLine("[GAME] : No output file from player " + _playerName); return(null); } try { var mazeFromPlayer = new Maze(playerOutputFilePath); return(mazeFromPlayer); } catch (UnreadableMazeException e) { Console.WriteLine(e.ToString()); logFile.WriteLine("[GAME] : Unreadable maze from player: " + _playerName); } return(null); }
private void CommandExec(CommandModel cmd) { Trace.Call(cmd); if (cmd.DataArray.Length < 2) { NotEnoughParameters(cmd); return; } var parameter = cmd.Parameter; var parameters = cmd.Parameter.Split(' '); var messageOutput = false; var executeOutput = false; if (parameters.Length > 0) { var shift = false; switch (parameters[0]) { case "-c": executeOutput = true; shift = true; break; case "-o": messageOutput = true; shift = true; break; } if (shift) { parameters = parameters.Skip(1).ToArray(); parameter = String.Join(" ", parameters); } } SysDiag.DataReceivedEventHandler handler = (sender, e) => { if (String.IsNullOrEmpty(e.Data)) { return; } // eat trailing newlines var output = e.Data.TrimEnd('\r', '\n'); if (executeOutput || messageOutput) { if (messageOutput && output.StartsWith(cmd.CommandCharacter)) { // escape command character output = String.Format("{0}{1}", cmd.CommandCharacter, output); } DoExecute(new CommandModel(cmd.FrontendManager, cmd.Chat, cmd.CommandCharacter, output)); } else { var msg = new MessageBuilder().AppendText(output).ToMessage(); AddMessageToFrontend(cmd, msg); } }; string file; string args = null; if (Environment.OSVersion.Platform == PlatformID.Unix) { file = "sh"; args = String.Format("-c \"{0}\"", parameter.Replace("\"", @"\""")); } else { file = parameters[1]; if (parameters.Length > 1) { args = String.Join(" ", parameters.Skip(1).ToArray()); } } var info = new SysDiag.ProcessStartInfo() { FileName = file, Arguments = args, RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false }; using (var process = new SysDiag.Process()) { process.StartInfo = info; process.OutputDataReceived += handler; process.ErrorDataReceived += handler; try { process.Start(); process.StandardInput.Close(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); process.WaitForExit(); } catch (Exception ex) { #if LOG4NET f_Logger.Error(ex); #endif var command = info.FileName; if (!String.IsNullOrEmpty(info.Arguments)) { command += " " + info.Arguments; } var msg = new MessageBuilder(). AppendErrorText("Executing '{0}' failed with: {1}", command, ex.Message). ToMessage(); AddMessageToFrontend(cmd, msg); } } }
public void AddDataReceivedEventHandler(System.Diagnostics.DataReceivedEventHandler outputDataReceivedProc) { UCI_Engine.OutputDataReceived += outputDataReceivedProc; }