2017-02-09 10 views
0

リダイレクトされた出力から重複したコンテンツに問題があります。process.OutputDataReceivedからの重複した出力

私のフォームには、実行とクリアという2つのボタンがあります。

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のラベルが重複しているように見えます。

これは何らかの種類のバッファに保存されていますか?もしそうなら、私はそれをクリアして再実行を許可しますか?

誰かがなぜこれが起こっているのか、それを解決する方法を説明することができますか?

+0

誰も知らないか、助けることができ???? –

+0

絶対に提案はありませんか?なぜそれが起こるかについての答えか????? –

答えて

0

私のためにそれを修正した職場の誰かに話をしました。だから、簡単笑

private void ClearBTN_Click(object sender, EventArgs e) 
    {   
    CMDpanel.Controls.Clear();      
    this.process.CancelOutputRead(); 
    this.process.Close(); 
    this.process.Refresh(); 
    this.process = new Process(); // this line resolved my issue!!         
    } 

}

関連する問題