我有一個richTextBoxLog
,它是公共和靜態的。c#從進程輸出異步捕獲並將其顯示在RichTextBox中
我宣佈它後,我開始一個新的線程,它初始化名爲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."
我嘗試使用此:
context.Post(新SendOrPostCallback(newMethod ),(對象)概率);
其中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進入大約一千次。
我試過了,但它沒有奏效。有沒有辦法在我的路上做到這一點,但是對於帖子的調用不是在循環結束後,而是在循環內? –