小编tip*_*nes的帖子

如何从另一个线程更新主线程中的文本框?

如何从运行不同类的新线程更新主线程中的文本框和标签.

MainForm.cs(主线程)

public partial class MainForm : Form
{
    public MainForm()
    {
        Test t = new Test();

        Thread testThread = new Thread(new ThreadStart(t.HelloWorld));
        testThread.IsBackground = true;
        testThread.Start();
    }

    private void UpdateTextBox(string text)
    {
        textBox1.AppendText(text + "\r\n");
    }

}

public class Test
{
    public void HelloWorld()
    {
        MainForm.UpdateTextBox("Hello World"); 
        // How do I execute this on the main thread ???
    }
}
Run Code Online (Sandbox Code Playgroud)

我看过这里的例子,但似乎无法做到正确.请有人给一些好的链接.

我再次开始新鲜,所以我不会搞砸我的代码.如果有人想用我的例子提出一个很好的例子.

此外,如果我不得不更新多个对象,如文本框和标签等(不是所有的同时),最好的方法是什么,为每个文本框设置一个方法,或者有一种方法可以用一种方法吗?

c# user-interface multithreading

5
推荐指数
2
解决办法
2万
查看次数

标签 统计

c# ×1

multithreading ×1

user-interface ×1