如何在 Windows 窗体中创建非阻塞控件?

发布于 2024-10-17 06:42:28 字数 810 浏览 0 评论 0 原文

在我的表单上,我有一个 RichTextBox,它将不断更新文本(聊天应用程序)。我还有一个文本框,用户可以在其中输入他们想要发送的文本。

无论如何,当 RichTextBox 更新时,它会阻塞 UI 线程,这意味着我无法在 TextBox 中键入内容(或使用任何按钮),直到它完成更新为止。同样,当 TextBox 处理按键时,RTB 会被阻止(如果用户按住/乱按按键,这可能会出现问题)。

建议的处理方法是什么?

这是相关代码(为了简洁起见,按代码流程的顺序进行了缩写):

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶花眉 2024-10-24 06:42:28

防止 UI 冻结的唯一方法是永远不要在 UI 线程上执行大量工作。我认为问题出在这里:

richTextBox1.Text += line + "\n"

乍一看,这似乎是一个很小的工作量:看起来您只要求 RichTextBox 附加一行。但是,如果您像这样使用它,RichTexBox看不到“追加一行”和“刷新所有内容”之间的区别,因为上面的代码与此等效:

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

它必须处理整个字符串每次。这就是为什么您应该使用 Text 属性rel="nofollow">附加文本

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文