2012-05-11 39 views
-1

我有一個用戶控件和兩個班我想打印我的Class1的結果爲usercontrol.I是從類利用該線路發送結果用戶控件拋出異常「跨線程操作無效」

((merge.MyControl)(MyControlInstance)).CLIDisplay = e.WorkItem.CustomerId; 

我的控制屬性顯示的結果是

public string CLIDisplay 
     { 
      get { return lblResultCLI.Text; } 
      set 
      { 
        lblResultCLI.Text = value; 

      } 
     } 

,但我得到以下情況例外,當我打電話一類我的C#形式

An exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll but was not handled in user code 

Additional information: Cross-thread operation not valid: Control 'tbxEvents' accessed from a thread other than the thread it was created on. 
+2

你還沒有解釋你在線程方面做了什麼,這顯然很重要。你對這個錯誤做了什麼研究? –

+0

錯誤是關於'tbxEvents',這是代碼中缺少的一種。 –

+0

[Cross-thread operation not valid:可從其創建的線程以外的線程訪問的控制]的可能重複(http://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control -ac-threads-other-than-the) – slugster

回答

7

你將不得不使用調用

this.Invoke((MethodInvoker) delegate 
{ 
    lblResultCLI.Text = value; 
}); 

下一次一定要使用谷歌...因爲lblResultCLI被另一個線程比你是一個創建發生

Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on

此錯誤運行你的代碼,這就是爲什麼你必須使用Invoke,以便訪問lblResultCLI控件的代碼在創建它的同一個線程上執行。

+2

老實說,這是一個很差的答案 - 你沒有做任何解釋**爲什麼** OP需要使用Invoke。 – slugster

+2

這是一個重複的,我爲什麼要打擾?我發佈了一個類似的問題鏈接到一些很好的解釋 – animaonline

+0

感謝它的工作...我之前研究過問題,但沒有得到我的問題解決.. –