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中出現重複標籤的新內容。
這是存儲在某種緩衝區?如果是這樣,我如何清除它讓我重新運行?
有人可以解釋爲什麼會發生這種情況,請問如何解決。
沒有人知道或能幫助???? –
絕對沒有建議?或回答爲什麼會發生????? –