C# StreamWriter doubling every line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Prodian
    New Member
    • Jan 2008
    • 16

    C# StreamWriter doubling every line

    The outputBox.Text is NOT getting double the lines, the Text file is getting every line of output double. The output is correct everything is working fine, but its writing everything twice in my log file. Can anybody see why? Thanks.

    Here is the code:
    Code:
           
    private void outPut()
     {
                
                Process process = new Process();
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.FileName = "gsengine.exe";
    
    
                process.OutputDataReceived +=
                new DataReceivedEventHandler(process_OutputDataReceived);
    
                process.Start();
                process.BeginOutputReadLine();
            }
                
    
                delegate void AddTextCallback(string text);
           
                void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
                {
                    this.AddText(e.Data);
    
                }
            
                private void AddText(string text)
                {
                    string path = "Log.txt";
    
                    using (StreamWriter sw = File.AppendText(path))
                    {
    
                        sw.Write(DateTime.Now + text + Environment.NewLine);
    
                        sw.Close();
                    }
    
                    if (this.outputBox.InvokeRequired)
                    {
                        AddTextCallback d = new AddTextCallback(AddText);
                        this.Invoke(d, new object[] { text });
                    }
    
                    else
                    {
    
                        this.outputBox.Text += text + Environment.NewLine;
    
                        outputBox.SelectionStart = outputBox.Text.Length;
                        outputBox.ScrollToCaret();
                    }
                    
            }
    Heres an example of the output:
    Code:
    2/8/2008 9:13:34 PMº                                                     º
    2/8/2008 9:13:34 PMº                                                     º
    2/8/2008 9:13:34 PMÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼCredits:                                    
    2/8/2008 9:13:34 PMÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼCredits:                                    
    2/8/2008 9:13:34 PM   - Prodian   - Startup/settings interface
    2/8/2008 9:13:34 PM   - Prodian   - Startup/settings interface
    2/8/2008 9:13:34 PM
    2/8/2008 9:13:34 PM
  • Prodian
    New Member
    • Jan 2008
    • 16

    #2
    so does anybody an idea whats wrong?

    Comment

    • Prodian
      New Member
      • Jan 2008
      • 16

      #3
      I still havent figured this one out, help please? Thanks!

      Comment

      • Prodian
        New Member
        • Jan 2008
        • 16

        #4
        I figured it out. Had the the stream writer in the wrong place. heres how i fixed it.
        Code:
        delegate void AddTextCallback(string text);
               
                    void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
                    {
                        this.AddText(e.Data);
        
                        string path = "Log.txt";
         
                        using (StreamWriter sw = File.AppendText(path))
                        {
         
                            sw.Write(DateTime.Now + text + Environment.NewLine);
         
                            sw.Close();
                        }
        
         
                    }
                
                    private void AddText(string text)
                    {
                         
                        if (this.outputBox.InvokeRequired)
                        {
                            AddTextCallback d = new AddTextCallback(AddText);
                            this.Invoke(d, new object[] { text });
                        }
         
                        else
                        {
         
                            this.outputBox.Text += text + Environment.NewLine;
         
                            outputBox.SelectionStart = outputBox.Text.Length;
                            outputBox.ScrollToCaret();
                        }
                        
                }

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Boy I take a day off and things get missed.
          Yes, right away I noticed your stream writer would get called twice (once for the initial and then once again on the invoked call).

          Good work on keeping at it until you found the problem.

          Comment

          Working...