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)));
}