请帮忙〜我迷路了 [英] Some help please ~ im lost
问题描述
好吧,所以我运行了一个打开.bat的程序,读取了它的输出,显示了它,并通过winform获得了给定的输入
Ok so im running a program that opens a .bat, reads its output, displays it and gets given input by the winform
这是我的代码:
Process p = new Process();
private void button1_Click(object sender, EventArgs e)
{
p.StartInfo.FileName = @"run.bat";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.EnableRaisingEvents = true;
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.Exited += new EventHandler(p_Exited);
p.Start();
p.BeginOutputReadLine();
}
void p_Exited(object sender, EventArgs e)
{
if (this.InvokeRequired)
{
Invoke(new EventHandler(p_Exited), new object[] { sender, e });
return;
}
textBox1.Text += "Server Has Shutdown Sucessfully ";
}
void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (this.InvokeRequired)
{
Invoke(new DataReceivedEventHandler(p_OutputDataReceived), new object[] { sender, e });
return;
}
textBox1.Text += (e.Data ?? string.Empty) + Environment.NewLine;
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();
}
private void button2_Click(object sender, EventArgs e)
{
p.StandardInput.WriteLine("shutdown");
p.Close();
}
}
}
我遇到的第一个问题是读取输出仅给我3行,然后连续给我:
First problem i get is the read output only gives me 3 lines then it continually gives me this:
>
>
>
>
>
ect ..
ect..
第二个问题,我尝试按我的停止"按钮,然后按注视的按钮(有效地重新启动)
Second problem, i try to press my stop button and then the stard button (effectivly re starting)
我收到此错误:
异步读取操作已在流上开始."
"An async read operation has already been started on the stream."
第三我想知道如何将textBox4的文本发送到正在运行的.bat
3rd I would like to know how i could get textBox4's Text to the .bat that im running
类似的东西应该可以正常工作:
something like this should work right:
private void textBox4_TextChanged(object sender, EventArgs e)
{
if (keydown == enter) {
p.StandardInput.WriteLine(textBox4.Text);
}
}
任何代码,摘要或帮助都将不胜感激
Any code, snippets or help would be much appreciated
推荐答案
It says in the documentation for Process.OutputDataReceived Event that:
正在处理异步输出的应用程序应调用 WaitForExit 方法,以确保已清除输出缓冲区.
The application that is processing the asynchronous output should call the WaitForExit method to ensure that the output buffer has been flushed.
有一个 这里有个合理的例子. 请注意,它将调用WaitForExit.
There's a reasonable example here. Note that it calls WaitForExit.
这篇关于请帮忙〜我迷路了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!