我有一个richTextBoxLog
这是公共和静态。两条线程通讯
我宣布它后,我开始一个新的线程,它初始化名为proba
的新变量。 richTextBoxLog
应该从proba
得到值。我想richTextBoxLog
初始化,而proba
正在初始化。
我想写类似:
richTextBoxLogFile.richTextBoxLog.Text += proba;
但是,当我把它写在由前面提到的线程调用的方法,我得到一个异常:"Cross-thread operation not valid: Control 'richTextBoxLog' accessed from a thread other than the thread it was created on."
我能写的东西像这样:停止线程,然后初始化richTextBoxLog
,然后再次启动线程,然后停止它,并且在初始化richTextBoxLog
结束时继续相同。
编辑:我尝试使用此:
context.Post(新SendOrPostCallback(newMethod),(对象)PROBA);
其中context是主线程的上下文,newMethod初始化richTextBoxLog的值。然而,这一呼吁是在由新线程启动方法,在一个while循环:
while (-1 != (ch = _reader.Read()))
{
Console.Write((char)ch);
proba += ((char)ch).ToString();
context.Post(new SendOrPostCallback(newMethod), (object)proba);
}
_complete.Set();
}
我现在的问题是,我想,当我在while循环进入newMethod被称为每次。但是,发生的事情是:while循环结束,并且在此之后newMethod进入大约一千次。
如果你想等待'proba'和richTextBoxLog初始化,然后让它们合作..你需要[同步](http://msdn.microsoft.com/en-us/library/ms173179.aspx)在初始化结束时标记并显示 – mtijn
问题是我在一个类中声明了richTextBoxLog,并且线程在另一个类中启动。 –