我从重定向控制台应用程序的标准输入中使用类时遇到问题
我可以执行控制台应用程序
var proc = new ConsoleAppManager("calc.exe");
proc.ExecuteAsync();
但是如何从控制台应用程序接收输出?我想我必须使用StandartTextReceived事件,但我不知道该怎么精确。有人可以给我一个示例代码来接收RichTextBox中的输出吗?
您确实需要订阅该StandartTextReceived
活动。这是一个示例,如何从button_click事件中执行此操作:
// class member on the form
ConsoleAppManager cm = null;
private void button1_Click(object sender, EventArgs e)
{
// check if have an instance, to prevent starting too much
if (cm == null)
{
cm = new ConsoleAppManager(@"cmd.exe");
} else
{
if (cm.Running)
{
// still running, bail out
return;
}
}
// subscribe to the event
// the implementation of the ConsoleAppManager handles UI thread sync
cm.StandartTextReceived += (s, text) =>
{
// it doesn't play nicely if the form
// closes while the process is still running
if (!this.richTextBox1.IsDisposed)
{
this.richTextBox1.AppendText(text);
}
};
cm.ExecuteAsync(@"/c ""dir c:\*.exe /s """);
}
// it is very limited to end it ...
private void Form3_FormClosed(object sender, FormClosedEventArgs e)
{
if (cm != null)
{
// how on earth do you end that process?
cm = null;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句