Corey
Corey

Reputation: 1195

How can I create a non-blocking control in Windows forms?

On my form, I have a RichTextBox that will be constantly updated with text (chat application). I also have a TextBox where the user types in their text that they want to send.

Anyway, when the RichTextBox is updating, it blocks the UI thread, meaning I cannot type in the TextBox (or use any buttons) until it finishes updating. Likewise, the RTB is blocked while the TextBox handles key presses (which may be a problem if a user is holding down/spamming keys).

What is the recommended way to deal with this?

Here's the relevant code (shortened for brevity, in order of code flow):

private void button1_Click(object sender, EventArgs e) {
    new Thread(new ThreadStart(() => Start())).Start();
}

public void Start() {
    irc = new IrcClient();
    irc.OnRawMessage += new IrcEventHandler(OnRawMessage);
    irc.Listen();
}

void OnRawMessage(object sender, IrcEventArgs e) {
    WriteLine(e.Data.RawMessage);
}

void WriteLine(string line) {
    this.BeginInvoke(new Action(() => richTextBox1.Text += line + "\n"));
    this.BeginInvoke(new Action(() => ScrollToEnd(richTextBox1)));
}

Upvotes: 2

Views: 1590

Answers (1)

Wim Coenen
Wim Coenen

Reputation: 66733

The only way to prevent the UI from freezing is to never do a large amount of work on the UI thread. I think the problem is here:

richTextBox1.Text += line + "\n"

At first sight this seems like a small amount of work: it looks like you only ask the RichTextBox to append a single line. However, the RichTexBox doesn't see the difference between "append one line" and "refresh everything" if you use it like that because the above code is equivalent to this:

richTextBox1.Text = richtTextBox1.Text + line + "\n"

It has to process the entire string each time. That's why instead of setting the Text property, you should use AppendText.

Upvotes: 3

Related Questions