2012-05-11 36 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

感谢它的工作...我之前研究过问题,但没有得到我的问题解决.. –