0
我遇到了重定向输出中的重复内容问题。process.OutputDataReceived的重复输出
在我的表单中,我有两个按钮:运行和清除。
public partial class BatchRun : Form
{
Process process = new Process();
public BatchRun()
{
InitializeComponent();
}
private void RunBTN_Click(object sender, EventArgs e)
{
//initiate the process
this.process.StartInfo.FileName = sMasterBATname;
this.process.StartInfo.UseShellExecute = false;
this.process.StartInfo.CreateNoWindow = true;
this.process.StartInfo.RedirectStandardOutput = true;
this.process.OutputDataReceived += new DataReceivedEventHandler(StandardOutputHandler);
this.process.StartInfo.RedirectStandardInput = true;
this.process.Start();
this.process.BeginOutputReadLine();
}
public void StandardOutputHandler(object sender, DataReceivedEventArgs outLine)
{
BeginInvoke(new MethodInvoker(() =>
{
Label TestLBL = new Label();
TestLBL.Text = text.TrimStart();
TestLBL.AutoSize = true;
TestLBL.Location = new Point(10, CMDpanel.AutoScrollPosition.Y + CMDpanel.Controls.Count * 20);
CMDpanel.Controls.Add(TestLBL);
CMDpanel.AutoScrollPosition = new Point(10, CMDpanel.Controls.Count * 20);
}));
}
private void ClearBTN_Click(object sender, EventArgs e)
{
CMDpanel.Controls.Clear();
this.process.CancelOutputRead();
this.process.Close();
this.process.Refresh();
}
}
如果我只想运行一次进程,即一旦进程完成就关闭表单,这很好用。
不过,我需要让用户重新运行同一程序或运行一个新的,因此我加入了一个明确的按钮,清除各种控件等
我遇到的问题是,点击清除按钮后, ,我想再次单击运行按钮而不关闭,然后运行sMAsterBAT文件(CMD)。
StandardOutputHandler似乎包含上一次运行的内容以及导致我的CMDpanel中出现重复标签的新内容。
这是存储在某种缓冲区?如果是这样,我如何清除它让我重新运行?
有人可以解释为什么会发生这种情况,请问如何解决。
没有人知道或能帮助???? –
绝对没有建议?或回答为什么会发生????? –