我有一个名为 mainForm 的类,它是我程序的主窗口。我在这个类中创建了一个 TextBox(这个 TextBox Logs 程序)对象,我想向它写入程序状态。我很容易从 mainForm 和其他对象(通过将 TextBox 对象传递给它)执行此操作,但是当我想从另一个线程执行此操作时,它很复杂。但是,我正在通过线程向 TextBox 写入它在 mainForm 中运行定义的代码(使用委托)。
我的问题是,如何在另一个类中运行的线程中写入 TextBox?
public partial class mainForm : Form
{
TextBox log = new TextBox();
.
.
.
OtherClass o = new OtherClass(log);
}
class OtherClass
{
private TextBox log;
public otherClass(TextBox aLog)
{
log = aLog;
Thread thread = new Thrad(new ThreadStart(this.run));
thread.Start();
}
private void run()
{
log.Text = "Message";// I Can't Do This. Can I Use Delegate Here? How?
}
}