468,545 Members | 1,718 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,545 developers. It's quick & easy.

C# StreamWriter doubling every line

16
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:
Expand|Select|Wrap|Line Numbers
  1.  
  2. private void outPut()
  3.  {
  4.  
  5.             Process process = new Process();
  6.             process.StartInfo.UseShellExecute = false;
  7.             process.StartInfo.RedirectStandardOutput = true;
  8.             process.StartInfo.RedirectStandardError = true;
  9.             process.StartInfo.FileName = "gsengine.exe";
  10.  
  11.  
  12.             process.OutputDataReceived +=
  13.             new DataReceivedEventHandler(process_OutputDataReceived);
  14.  
  15.             process.Start();
  16.             process.BeginOutputReadLine();
  17.         }
  18.  
  19.  
  20.             delegate void AddTextCallback(string text);
  21.  
  22.             void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
  23.             {
  24.                 this.AddText(e.Data);
  25.  
  26.             }
  27.  
  28.             private void AddText(string text)
  29.             {
  30.                 string path = "Log.txt";
  31.  
  32.                 using (StreamWriter sw = File.AppendText(path))
  33.                 {
  34.  
  35.                     sw.Write(DateTime.Now + text + Environment.NewLine);
  36.  
  37.                     sw.Close();
  38.                 }
  39.  
  40.                 if (this.outputBox.InvokeRequired)
  41.                 {
  42.                     AddTextCallback d = new AddTextCallback(AddText);
  43.                     this.Invoke(d, new object[] { text });
  44.                 }
  45.  
  46.                 else
  47.                 {
  48.  
  49.                     this.outputBox.Text += text + Environment.NewLine;
  50.  
  51.                     outputBox.SelectionStart = outputBox.Text.Length;
  52.                     outputBox.ScrollToCaret();
  53.                 }
  54.  
  55.         }
  56.  
Heres an example of the output:
Expand|Select|Wrap|Line Numbers
  1. 2/8/2008 9:13:34 PMบ                                                     บ
  2. 2/8/2008 9:13:34 PMบ                                                     บ
  3. 2/8/2008 9:13:34 PMศอออออออออออออออออออออออออออออออออออออออออออออออออออออผCredits:                                    
  4. 2/8/2008 9:13:34 PMศอออออออออออออออออออออออออออออออออออออออออออออออออออออผCredits:                                    
  5. 2/8/2008 9:13:34 PM   - Prodian   - Startup/settings interface
  6. 2/8/2008 9:13:34 PM   - Prodian   - Startup/settings interface
  7. 2/8/2008 9:13:34 PM
  8. 2/8/2008 9:13:34 PM
  9.  
Feb 9 '08 #1
4 4513
Prodian
16
so does anybody an idea whats wrong?
Feb 10 '08 #2
Prodian
16
I still havent figured this one out, help please? Thanks!
Feb 13 '08 #3
Prodian
16
I figured it out. Had the the stream writer in the wrong place. heres how i fixed it.
Expand|Select|Wrap|Line Numbers
  1. delegate void AddTextCallback(string text);
  2.  
  3.             void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
  4.             {
  5.                 this.AddText(e.Data);
  6.  
  7.                 string path = "Log.txt";
  8.  
  9.                 using (StreamWriter sw = File.AppendText(path))
  10.                 {
  11.  
  12.                     sw.Write(DateTime.Now + text + Environment.NewLine);
  13.  
  14.                     sw.Close();
  15.                 }
  16.  
  17.  
  18.             }
  19.  
  20.             private void AddText(string text)
  21.             {
  22.  
  23.                 if (this.outputBox.InvokeRequired)
  24.                 {
  25.                     AddTextCallback d = new AddTextCallback(AddText);
  26.                     this.Invoke(d, new object[] { text });
  27.                 }
  28.  
  29.                 else
  30.                 {
  31.  
  32.                     this.outputBox.Text += text + Environment.NewLine;
  33.  
  34.                     outputBox.SelectionStart = outputBox.Text.Length;
  35.                     outputBox.ScrollToCaret();
  36.                 }
  37.  
  38.         }
  39.  
Feb 14 '08 #4
Plater
7,872 Expert 4TB
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.
Feb 14 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Joecx | last post: by
1 post views Thread by Lars Hansen | last post: by
13 posts views Thread by Rob Corwin | last post: by
5 posts views Thread by Rob Corwin | last post: by
9 posts views Thread by ShadowOfTheBeast | last post: by
2 posts views Thread by =?Utf-8?B?cmF1bGF2aQ==?= | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.