System.InvalidOperationExcepton:跨线程操作无效 [英] System.InvalidOperationExcepton: Cross-thread operation not valid

查看:101
本文介绍了System.InvalidOperationExcepton:跨线程操作无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我这里有一个使用C#语言编写的独立应用程序.
当我按下计算"按钮时发生错误
请查看以下错误:

错误说明:System.InvalidOperationExcepton:跨线程操作无效:从创建该线程的线程以外的其他线程访问控件"label1".
在system.windows.forms.control.get_Handle()
在System.Windows.Forms.Control.set_WindowText(String value)
在System.Windows.Forms.Control.set_text(String Value)
在D:\ PROCUREMENT SYSTEMS BACKUP \ TOOLS AND INSTALLERS \ PROJECTS \ BCCS Tool \ Source Code \ BCCSReportGenerator \ frmComputeBCCS.cs第62行的BCCSReportGenerator.frmComputeBCCS.GenerateReport()中
十分感谢您的帮助.

Hello everyone, I have here a standalone application written using C# language.
Error occured when I hit the button "compute"
Please see below error:

Error Description : System.InvalidOperationExcepton: Cross-thread operation not valid: Control ''label1'' accessed froma thread other than the thread it was created on.
at system.windows.forms.control.get_Handle()
at System.Windows.Forms.Control.set_WindowText(String value)
at System.Windows.Forms.Control.set_text(String Value)
at BCCSReportGenerator.frmComputeBCCS.GenerateReport() in D:\PROCUREMENT SYSTEMS BACKUP\TOOLS AND INSTALLERS\PROJECTS\BCCS Tool\Source Code\BCCSReportGenerator\frmComputeBCCS.cs line 62

Thank you very much in advance for your help.

推荐答案

您只能使用UI线程中的任何控件:创建该控件的线程.如果您使用的线程不正确,则需要调用正确的线程.

如何:对Windows窗体控件进行线程安全调用 [ ^ ]

[edit]错字:已创建"的受控"-OriginalGriff [/edit]
You can only use any control from the UI thread: the thread it was created on. If you aren''t on the right thread, then you need to invoke the correct one.

How to: Make Thread-Safe Calls to Windows Forms Controls [^]

[edit]Typo: "controlled" for "created" - OriginalGriff[/edit]


在要更新label1的函数中,请执行以下操作:

In the function where you are updating label1, do this:

this.Invoke(new MethodInvoker(UpdateLabel)); //UpdateLabel is a function where the label will be updated

private void UpdateLabel()
{
 //Do something with the label1
}


您无法通过非UI线程在UI中调用任何内容.您可以使用System.Threading.DispatcherBeginInvokeInvoke(首选使用表格和WPF)或System.Windows.Forms.Control(显然仅使用表格).

请参阅我过去的回答中的详细说明和示例代码:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
You cannot call anything in UI from a non-UI thread. You can use BeginInvoke or Invoke of System.Threading.Dispatcher (both forms and WPF, preferred) or System.Windows.Forms.Control (Forms only, apparently).

See detailed explanation and samples code in my past asnwers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

More links on thread use:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于System.InvalidOperationExcepton:跨线程操作无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆