views:

211

answers:

2

I am reading data from a stream (provided by a process) character by character and adding it to a textbox so the user can see. The only problem is, this is SLOW. The user needs to see the data as it is given to the program (with little to no delay). I would like something like how terminals handle text, it can scroll by so fast that it's a blur.

How can I improve this?

For reference, I'm using C# .net3.5 and winforms.

A: 

Some code would help to figure out what the bottleneck is.

That said, I'd try something along these lines (I wouldn't suggest copy/paste as I can't test it here):

// Stream s...
byte[] buffer = new buffet[bufferSize];
s.BeginRead(b, 0, buffer.Length,
    delegate
        {
            if (textBox1.InvokeRequired)
            {
                textBox1.Invoke(
                    new MethodInvoker(
                        delegate 
                        { 
                            textBox1.Text = Encoding.Unicode.GetString(b); 
                        }));
            }
            else
            {
                textBox1.Text = Encoding.Unicode.GetString(b);
            }
         }, null);
SnOrfus
+2  A: 

The Text property of the text box is a string, and strings are immutable (meaning that you can't change a string). This means that each time that you add a character, you will be creating a new copy of the string with one character added at the end.

If you for example have 10000 characters in the textbox, you will be copying 20kB of data to add the next character. Adding a hundred characters one at a time means copying 2MB of data.

If the data is line based, use a list instead of a text box, so that you only have to update the last line when you add a character.

Guffa