I've looked everywhere, but I just can't find the right answer.
I use VS2013 > C# > Windows Forms-Application
Below you see a working version of my process. But I have two little problems, which I don't know how to fix.
The *.exe is an optimization algorithm, that displays each iteration it does and the current best solution it has found. -> but because I have 'useshellexecute = false' I don't see anything in the command shell
The user can interupt the algorithm at any time by pressing 'Ctrl+C' and the algorithm will stop and return the current best solution -> but because I have 'useshellexecute = false' I can't imput any key-commands
How can I fix this ?? - I need to see the interations and to be able to press 'Ctrl+C'. - It doesn't has to be in the command shell, I would be ok with an alternativ "interface". - If I set 'useshellexecute = true' how can I input commands and read all lines.
Please note:
P.StartInfo.Arguments
to input the commands does not work. The *.exe will thow an "invalite input" error.
Code that works:
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);
}
edit 11.11.2014 / Threaded Process / Output in 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);
}));
}
}