如何从运行不同类的新线程更新主线程中的文本框和标签.
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)
我看过这里的例子,但似乎无法做到正确.请有人给一些好的链接.
我再次开始新鲜,所以我不会搞砸我的代码.如果有人想用我的例子提出一个很好的例子.
此外,如果我不得不更新多个对象,如文本框和标签等(不是所有的同时),最好的方法是什么,为每个文本框设置一个方法,或者有一种方法可以用一种方法吗?