控制'richTextBox1'从c#创建的线程以外的线程访问c# [英] Control 'richTextBox1' accessed from a thread other than the thread it was created on in c#

查看:143
本文介绍了控制'richTextBox1'从c#创建的线程以外的线程访问c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们。我正在从richTextBox

hi friends. i'm retreiving data from the richTextBox


中检索数据。但是我得到了上面的错误。我可以解决这个问题吗

. but i got above error.how can i solve this

推荐答案

将UI处理委托给创建UI对象的线程。这通常是主线程,也就是GUI线程。
Delegate the UI handling to the thread that created the UI object. That is usually the main thread, a.k.a. GUI thread.
// Call this from whatever thread you like.
void SetTextBoxText(RichTextBox box, string text)
{
    // Accessing a GUI object from another thread than GUI is not permitted
    //   There are three exceptions to that: InvokeRequired, BeginInvoke, Invoke
    if (box.InvokeRequired)
    // Called from another thread than GUI thread.
    //   Delegate work to GUI thread and exit.
    {
        box.Invoke(new Action<string>(SetTextBoxText), new object[] { box, text });
        return;
    }
    // We're in GUI thread now. Accessing the GUI object is safe.
    box.Text = text;
}


这篇关于控制'richTextBox1'从c#创建的线程以外的线程访问c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆