public void Run() { if (RunAll) { CurProgramNum = 0; } object[] prResult = new object[uePrograms.Length]; for (int i = 0; i < uePrograms.Length; i++) { ProgramsRunCmdLine( "[" + (CurProgramNum + 1).ToString() + "/" + (MaxProgramNum + 1).ToString() + "] " + uePrograms[CurProgramNum] + " " + ueProgramsArgs[CurProgramNum], new EventArgs()); ProgramResult prgResult = exeProcessWithRslt(); prResult[i] = prgResult; if (prgResult.exitCode != 0) { break; //错误就停止 } execProgramIteration(); } ProgramsRunResult(prResult, new EventArgs()); }
private ProgramResult exeProcessWithRslt() { string uelogs = ""; string execPath = uePrograms[CurProgramNum]; string args = ueProgramsArgs[CurProgramNum]; using (Process Rprocess = new Process()) { System.Timers.Timer execTimer = new System.Timers.Timer(); execTimer.Interval = __maxRunTime * 1000; execTimer.Elapsed += delegate(object ta, ElapsedEventArgs te) { execTimer.Stop(); try { if (!Rprocess.HasExited) { Rprocess.Kill(); } } catch (System.Exception) { }; }; Rprocess.StartInfo.UseShellExecute = false; //不使用CMD Rprocess.StartInfo.CreateNoWindow = true; //不显示黑色窗口 Rprocess.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e) { string logs = ""; try { logs = e.Data; } catch (System.Exception) { } uelogs = uelogs + "\n" + logs; ProgramsRunLogStepRs(logs, new EventArgs()); }; Rprocess.StartInfo.RedirectStandardOutput = true; Rprocess.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e) { string logs = ""; try { logs = e.Data; } catch (System.Exception) { } uelogs = uelogs + "\n" + logs; ProgramsRunLogStepRs(logs, new EventArgs()); }; Rprocess.StartInfo.RedirectStandardError = true; Rprocess.StartInfo.FileName = execPath; Rprocess.StartInfo.Arguments = args; Rprocess.EnableRaisingEvents = true; execTimer.Enabled = true; execTimer.Start(); if (!Rprocess.Start()) { return(new ProgramResult(execPath, args, -1, Terminated(), MaxProgramNum - CurProgramNum)); } Rprocess.BeginOutputReadLine(); Rprocess.BeginErrorReadLine(); Rprocess.WaitForExit(); ProgramResult pResult = new ProgramResult(execPath, args, Rprocess.ExitCode, Terminated(), MaxProgramNum - CurProgramNum); pResult.execLogs = uelogs; return(pResult); } }
private void CpQueue_ProgramsRunResult(object reports, EventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; //跨线程操作 Object[] execReports = (Object[])reports; if (cb_AutoClose.Checked) { logsBox.Clear(); } logsBox.AppendText("Runned!\r\n"); for (int i = 0; i < execReports.Length; i++) { wTModule.ProgramResult oReport = (wTModule.ProgramResult)execReports[i]; if (oReport.exitCode != 0) { //写入日志框 int logsBoxCount = logsBox.Text.Length; string programExecParam = "[" + (i + 1).ToString() + "/" + execReports.Length + "] Error: " + oReport.execName + " " + oReport.execArgs + "\r\n"; string programExitCodeParam = "Error With Exit Code: " + oReport.exitCode.ToString() + "\r\n"; logsBox.AppendText(programExecParam); logsBox.AppendText(programExitCodeParam); logsBox.AppendText(oReport.execLogs); //错误运行 logsBox.Select(logsBoxCount, programExecParam.Length + programExitCodeParam.Length - 4); logsBox.SelectionColor = Color.Red; //标红 logsBox.SelectionStart = logsBoxCount; logsBox.ScrollToCaret(); //移动光标到错误位置 Cursor = Cursors.Default; btn_gen.Enabled = true; //恢复按钮 return; //停止运行 } } if (!File.Exists(__targetImgFile)) { for (int i = 0; i < execReports.Length; i++) { wTModule.ProgramResult oReport = (wTModule.ProgramResult)execReports[i]; //写入日志框 int logsBoxCount = logsBox.Text.Length; logsBox.AppendText("[" + (i + 1).ToString() + "/" + execReports.Length + "] Error: " + oReport.execName + " " + oReport.execArgs + "\r\n"); logsBox.AppendText("Error With Exit Code: " + oReport.exitCode.ToString() + "\r\n" + oReport.execLogs); } ; logsBox.AppendText("Failed! File Not Found: \r\n" + __targetImgFile); Cursor = Cursors.Default; logsBox.ForeColor = Color.Red; //标红 btn_gen.Enabled = true; //恢复按钮 return; //停止运行 } logsBox.Select(logsBox.Text.Length, 0); logsBox.ScrollToCaret(); Microsoft.Office.Interop.Word.Document ThisDoc = Globals.ThisAddIn.Application.ActiveDocument; //完成运行队列 int shapePosition = 0; InlineShape inDocPic; if (ThisDoc.Application.Selection.Type != WdSelectionType.wdSelectionIP) { shapePosition = ThisDoc.Application.Selection.Font.Position; ThisDoc.Application.Selection.Delete(); //删除选中 的数据 } if (Ribbon.get_param_value(Ribbon.settingsBox.program_exec_params, "grapher") == (string)"dvipng") { //dvipng 产出PNG为72dpi,将生成的png图片转换分辨率 string pngvRes = Ribbon.get_param_value(Ribbon.settingsBox.program_exec_params, "pngvRes"); int pngdpi = int.Parse(pngvRes); Bitmap bMp = (Bitmap)Image.FromFile(__targetImgFile); bMp.SetResolution(pngdpi, pngdpi); string R_imgFile = workPath + "\\param" + "_" + pngvRes + ".png"; bMp.Save(R_imgFile, ImageFormat.Png); bMp.Dispose(); inDocPic = ThisDoc.InlineShapes.AddPicture(R_imgFile); } else { inDocPic = ThisDoc.InlineShapes.AddPicture(__targetImgFile); //svg直接插入 } inDocPic.AlternativeText = __texContent; //写入Tex数据 inDocPic.Select(); //选择新插入的图片 ThisDoc.Application.Selection.Font.Position = shapePosition; //基线还原 if (cb_AutoClose.Checked) //检测自动关闭 { this.Close(); } Cursor = Cursors.Default; btn_gen.Enabled = true; //复原按钮 return; }