Over the time the CPU utilization starts increasing running my application

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sudhanshunagar
    New Member
    • Mar 2010
    • 1

    Over the time the CPU utilization starts increasing running my application

    I have an application which listens to the serial port, processes the data and puts the extracted data on a large text box (which automatically scrolls to bottom).

    The application even logs the same data in a text file (using FileStream - StreamWriter)

    Over the time the CPU utilization starts increasing and after running it for 15 hrs its almost 100%.

    I suspect the problem could be due to:
    1.

    Code:
    txtTraffic.AppendText(sTextToWrite);
    txtTraffic.AppendText(Environment.NewLine);
    txtTraffic.SelectionStart = txtTraffic.Text.Length;
    txtTraffic.ScrollToCaret();
    these lines of code that executes every 250 mSec other than writing the same data onto a text file (after 15 hrs the log file size was 8 Meg)

    2. FileStream - StreamWriter could be the problem although I highly doubt that
    Last edited by sudhanshunagar; Mar 5 '10, 11:05 PM. Reason: To be more specific
  • EARNEST
    New Member
    • Feb 2010
    • 128

    #2
    .Text += string; is very expensive (re-write the string from memory, back to memory). If you need to add a line only, use .AppendText(str ing);
    ---
    So..
    In 1hr your string would be 1mb. And you will need to transfer that meg every time you try to .Text += string;
    In 2hrs 1mb+new size to transfer...
    In 10hrs all the size...etc.
    It would be very "heavy".
    ---

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      I see this one a lot.
      Instead of a Textbox and appending the text (as mentioned is expensive), using a ListView, set to Details. Then add to it it's .Items collection. Now you can pick any entry you want (if you need to) by selecting myListView.Item s[15] or whatever.

      The other good thing about this is you can delete off items as the ListView fills up.

      if (ListView.Items .Count > 500) ListVIew.Item.R emoveAt(0);

      So every time you add, you check, and remove the oldest items as needed.

      Also, with your log file: Are you keeping it open for 15 hours? Is the entire file therefore in memory the entire time? If so... Bad. Very easy for the file to get corrupted if the PC shuts down without closing it properly.
      Just us a File.Append to open the file, add to it, close it.

      Comment

      Working...