【发布时间】:2014-12-30 08:34:42
【问题描述】:
我到处寻找,但找不到正确的答案。
我使用 VS2013 > C# > Windows Forms-Application
您可以在下面看到我的流程的工作版本。 但是我有两个小问题,我不知道如何解决。
*.exe 是一种优化算法,它显示它执行的每次迭代以及它找到的当前最佳解决方案。 -> 但因为我有 'useshellexecute = false' 我在命令 shell 中看不到任何内容
用户可以随时按“Ctrl+C”中断算法,算法将停止并返回当前最佳解 -> 但因为我有 'useshellexecute = false' 我不能输入任何键命令
我该如何解决这个问题?? - 我需要查看交互并能够按“Ctrl+C”。 - 它不必在命令外壳中,我可以使用替代“界面”。 - 如果我设置'useshellexecute = true',我如何输入命令并读取所有行。
请注意:
P.StartInfo.Arguments
输入命令不起作用。 *.exe 将抛出“无效输入”错误。
有效的代码:
private void btn_Optimize_Start_Click(object sender, EventArgs e)
{
Process P = new Process();
P.StartInfo.FileName = @Application.StartupPath + @"\Algorithm.exe";
P.StartInfo.UseShellExecute = false;
P.StartInfo.RedirectStandardInput = true;
P.StartInfo.RedirectStandardOutput = true;
P.StartInfo.RedirectStandardError = true;
P.Start();
//sets timelimit 30 min
P.StandardInput.WriteLine("set lim tim 1800");
//reads the modell for which an optimal solution has to be found
P.StandardInput.WriteLine("read modell.zpl");
//command that starts the optimization algorithm
P.StandardInput.WriteLine("optimize"); //this part can take hours
//command that displays the solution
P.StandardInput.WriteLine("display solution");
//ends the *.exe
P.StandardInput.WriteLine("quit");
//saves all information in a log-file with which I can work
string[] log = P.StandardOutput.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
//opens a function, that formats the solution
this.result_create(log);
}
编辑 11.11.2014 / 线程化进程 / RichTextBox 中的输出:
private void btn_Optimize_Start_Click(object sender, EventArgs e)
{
Process P = new Process();
P.StartInfo.FileName = @Application.StartupPath + @"\Algorithm.exe";
P.StartInfo.UseShellExecute = false;
P.StartInfo.RedirectStandardInput = true;
P.StartInfo.RedirectStandardOutput = true;
P.StartInfo.RedirectStandardError = true;
//*** NEW *** Event Handler for the asynchron Output-Process
P.OutputDataReceived += new DataReceivedEventHandler(this.Asyn_Process);
P.Start();
//*** NEW *** Starts asynchron Output-Process
P.BeginOutputReadLine();
//sets timelimit 30 min
P.StandardInput.WriteLine("set lim tim 1800");
//reads the modell for which an optimal solution has to be found
P.StandardInput.WriteLine("read modell.zpl");
//command that starts the optimization algorithm
P.StandardInput.WriteLine("optimize"); //this part can take hours
//command that displays the solution
P.StandardInput.WriteLine("display solution");
//ends the *.exe
P.StandardInput.WriteLine("quit");
//*** DELETED ***
//saves all information in a log-file with which I can work
//string[] log = P.StandardOutput.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
//opens a function, that formats the solution
//this.result_create(log);
}
//*** NEW *** The asynchronous Process
private void Asyn_Process(object sender, DataReceivedEventArgs e)
{
if (this.rTB_Log.InvokeRequired && e.Data != null)
{
//Anonym Invoke Function
this.rTB_Log.Invoke(new MethodInvoker(delegate()
{
//Writes Output continuously in the RichTextBox
this.rTB_Log.Text += e.Data + Environment.NewLine;
//Scroll to End of RichTextBox continuously
this.rTB_Log.SelectionStart = this.rTB_Log.Text.Length;
this.rTB_Log.ScrollToCaret();
}));
}
//When the process has finished (e.Data == null)
else
{
//Anonym Invoke Function
this.rTB_Log.Invoke(new MethodInvoker(delegate()
{
//Saves the RichTextBox-Content in a Text-File
this.rTB_Log.SaveFile(Algorithm.log", RichTextBoxStreamType.PlainText);
}));
}
}
【问题讨论】:
-
去掉
InvokeRequired。当您不需要时调用Invoke不是错误,跳过该逻辑将是错误。
标签: c# input process output invoke