2011-09-28 27 views
0

我有一個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進入大約一千次。

回答

相關問題