473,898 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# StreamWriter doubling every line

16 New Member
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 5118
Prodian
16 New Member
so does anybody an idea whats wrong?
Feb 10 '08 #2
Prodian
16 New Member
I still havent figured this one out, help please? Thanks!
Feb 13 '08 #3
Prodian
16 New Member
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 Recognized Expert Expert
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

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

Similar topics

2
2441
by: Joecx | last post by:
Using vb.net, I am using Streamreader to read a text file and searching for a line to delete, the I close the file and open it as a streamwriter so I can put the new file back to disk without the line that I deleted. Can someone tell me how I can do this without having to close the file first before switching to streamwriter, because closing the file allows someone else to grab the file before I put the new information back. Thanks if any...
1
7633
by: Lars Hansen | last post by:
Hi This is probably pretty basic, but I have a problem with the access-level (local variable), when creating a new StreamWriter. I am trying to write some price information to a textfile - which works fine. But due to the dataamounts involved I now need to split it out to several files. The actions I am taking are: - create the first file
13
5725
by: Rob Corwin | last post by:
Hi, a c# app of mine that parses 30,000 xml files writes large amounts of data to file (> 2GB) using a streamwriter object (sw). everything works fine except that the memory used by the app grows linearly during execution and eventually crashes the computer. without going into too much detail of my code, i made the following observations: - if you comment out the sw.Write(x) statement (which is inside the loop that parses the xml...
5
4254
by: Rob Corwin | last post by:
Hi, a c# app of mine that parses 30,000 xml files writes large amounts of data to flat file (> 2GB) using a streamwriter object (sw). everything works fine except that the memory used by the app grows linearly during execution and eventually crashes the computer. without going into too much detail of my code, i made the following observations: - if you comment out the sw.Write(x) statement (which is inside the loop that parses the xml...
9
4605
by: ShadowOfTheBeast | last post by:
Hi, I have got a major headache understanding streamReader and streamWriter relationship. I know how to use the streamreader and streamwriter independently. but how do you write out using the streamwriter, what you have read into a streamReader? and also can someone explain how they work in simple terms -- The Matrix Insurrection
4
20571
by: rex64 | last post by:
I am getting an error message and I have not been able to figure hot how to fix it. I have done some research with no answers yet. I found this code that may help? Not sure what to do with it. using (StreamWriter sw = new StreamWriter (fullAddress , false, Encoding.UTF7)) Error message::::::::::::::::::::::::::::::::::::::::::::::
3
3736
by: Don | last post by:
I have a strange bug popping up every once in a while. I've got a section of code that executes these statements when working with a streamwriter: --- Start --- .... ' Close the streamwriter _fileWriter.Close()
12
11156
by: Nina | last post by:
Hi there, What is the maximum length that one line can hold in a text file using StreamWriter's Write or WriteLine method? If the string is too long for one line to hold, what will Write or WriteLine do? In order me to retrieve data correctly from the text file later, I have to know the right index. For the following code,if str1's length exceeds one line in text file, how can I get str2's index when reading the text file? Is there...
2
2127
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
vs2008 c3 HI: I am using streamwriter to write few lines. I need to know what was was written to each line, I am closing streamWriter after ea line (otherwise line is not output) ....then for the next line I need to reopen it. How? or how do I check the line in the file without closing streamwriter? thanks
0
9993
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10858
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10948
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10484
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8036
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6077
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3305
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.