从控制台应用程序发送的输入/输出得到(C#/的WinForms) [英] Sending input/getting output from a console application (C#/WinForms)
问题描述
我有3控件的形式:
- 一个文本框供用户输入
命令发送到控制台
应用程序, - A键来确认命令
发送和 - 只读文本框来显示
从应用程序的输出。
我要的是为用户在第一个文本框中输入命令,preSS按钮进入,并通过第二个文本框的反馈。
What I want is for the user to enter commands in the first textbox, press the button to enter and receive feedback via the second textbox.
我知道如何使用 ProcessStartInfo.RedirectStandardOutput
但是,但是,应用程序时,我使用挂起<code> StandardOutput.ReadToEnd()。
I know how to use ProcessStartInfo.RedirectStandardOutput
but, however, the app hangs when I use StandardOutput.ReadToEnd()
.
我看了一下异步 Process.BeginOutputReadLine()
但是,即使我的应用程序不挂,不知何故,我得到在文本框中没有反应,它绝对什么都没有。
I had a look at the asynchronous Process.BeginOutputReadLine()
but, even though my app does not hang, somehow I get no response in the textbox, it does absolutely nothing.
下面是我的code:
public partial class MainForm : Form
{
private void MainForm_Load(object sender, EventArgs e)
{
InitializeInterpreter();
}
private void InitializeInterpreter()
{
InterProc.StartInfo.UseShellExecute = false;
InterProc.StartInfo.FileName = "app.exe";
InterProc.StartInfo.RedirectStandardInput = true;
InterProc.StartInfo.RedirectStandardOutput = true;
InterProc.StartInfo.RedirectStandardError = true;
InterProc.StartInfo.CreateNoWindow = true;
InterProc.OutputDataReceived += new DataReceivedEventHandler(InterProcOutputHandler);
InterProc.Start();
}
private static void InterProcOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
{
OutputTextBox.Append(Environment.NewLine + outLine.Data);
}
}
private void Enterbutton_Click(object sender, EventArgs e)
{
InterProc.StandardInput.Write(CommandtextBox.Text);
InterProc.BeginOutputReadLine();
}
}
有什么办法,我可以顺利地有此运行?谢谢你。
Is there any way I can have this run smoothly? Thanks.
推荐答案
如果你想要的东西互动,我得到这个code的工作(你的修改,细节下面的修改)
If you want something interactive, I got this code to work (yours modified, details on modifications below)
private void InitializeInterpreter()
{
InterProc.StartInfo.UseShellExecute = false;
InterProc.StartInfo.FileName = "Echoer.exe";
InterProc.StartInfo.RedirectStandardInput = true;
InterProc.StartInfo.RedirectStandardOutput = true;
InterProc.StartInfo.RedirectStandardError = true;
InterProc.StartInfo.CreateNoWindow = true;
InterProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
InterProc.OutputDataReceived += new DataReceivedEventHandler(InterProcOutputHandler);
bool started = InterProc.Start();
InterProc.BeginOutputReadLine();
}
private void AppendTextInBox(TextBox box, string text)
{
if (this.InvokeRequired)
{
this.Invoke((Action<TextBox, string>)AppendTextInBox, OutputTextBox, text);
}
else
{
box.Text += text;
}
}
private void InterProcOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
AppendTextInBox(OutputTextBox, outLine.Data + Environment.NewLine);
}
private void Enterbutton_Click(object sender, EventArgs e)
{
InterProc.StandardInput.WriteLine(CommandTextBox.Text);
}
所以,我搬到了BeginOutputReadLine的过程开始之后。这样就保证它真的只调用一次。我也做了清理线程调用所需的调用。这应该可以为你工作。
So, I moved the BeginOutputReadLine to just after the process is started. That ensures it's really only called once. I also did an invoke required to clean up thread calls. Hopefully this should work for you.
这篇关于从控制台应用程序发送的输入/输出得到(C#/的WinForms)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!