微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Crossthread操作无效… – VB.NET

我正在使用vb.net,在我的程序中,当我运行我的后台工作程序时,我会得到这个’crossthread操作无效’的错误,这将使此文本框启用为真.我的主子将首先将启用为false,当后台工作程序运行时,它会将其重新设置为true,然后退出.为什么会给我一个错误? FYI:有更多的代码,但我不想让它更混乱…

这是堆栈跟踪:

at System.Windows.Forms.Control.get_Handle()
   at System.Windows.Forms.Control.OnEnabledChanged(EventArgs e)
   at System.Windows.Forms.Control.set_Enabled(Boolean value)
   at Helium.Form1.BackgroundWorker1_DoWork(Object sender,DoWorkEventArgs e) in C:\Users\Kevin\documents\visual studio 2010\Projects\Helium\Helium\Form1.vb:line 167
   at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
   at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)

这里是确切的错误信息:

{"Cross-thread operation not valid: Control 'mainText' accessed from a thread other than the thread it was created on."}

有人可以帮我出来!

谢谢,

凯文

BackgroundWorker类的目的是在GUI保持响应时对非GUI线程执行工作.除非您将Control.CheckForIllegalCrossthreadCalls设置为false(您不应该执行此操作),或者使用其他答案(我也不推荐)中的建议使用Invoke,那么您将获得非法的跨线程操作异常.

如果您希望在BackgroundWorker运行时发生GUI相关的“东西”,我通常建议使用BackgroundWorker.ReportProgress方法,并将适当的处理程序附加到BackgroundWorker.ProgressChanged事件.如果您希望在BackgroundWorker完成后发生一些事情,那么请简单地将您的处理程序附加到BackgroundWorker.RunWorkerCompleted事件中.

原文地址:https://www.jb51.cc/vb/255155.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐