跨线程操作无效:在BACKGROUNDWORKER&中实例化一个表单。尝试以其他形式使用它时出错 [英] cross thread operation not valid:Instantiating a form in BACKGROUNDWORKER & getting error when trying to use it in other form

查看:52
本文介绍了跨线程操作无效:在BACKGROUNDWORKER&中实例化一个表单。尝试以其他形式使用它时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个表单需要花费很多时间来加载,因为我正在使用的类是在表单加载之前被实例化的。

现在表单需要加载,我显示使用后台工作程序加载图像(在表单上)。

现在,在原始表单在后台工作程序中被删除后,我试图在不同的函数中显示表单,我将其分配给MDIPArent表单。

这里我得到错误''交叉线程操作无效:控制''表单''从一个线程访问,而不是在''上创建的线程。

亲切的帮助,因为其他工作正常而且很好..

感谢你的期待。

one of my form takes hell lot of time to load, reason being the class i am using is intantiated before the form could load.
now for the time the form takes to load, i display a loading image(on a form) using background worker.
Now after the original form is intatiated in the backgroundworker, i am trying to show the form in a different function where i assign it to an MDIPArent form.
here i get the error ''cross thread operation not valid: control ''form'' accessed from a thread other than the thread it wasa created on''.
kindly help as evrything else is working fine xcept dis..
thanking you in anticipation.

推荐答案

要访问在background-worker中创建的控件,请使用控件的调用方法 -



代码在backgroundworker的DO_WORK()方法中/>


''form_name是传递给Ru的字符串参数nWorkerAsync方法

dim frm as form = activator.createinstance(form_name,form)



''hereme是d的MDIPARENT形式frm将在下面的方法中加入



Me.Invoke(新动作(函数()

ShowForm(frm) ''ShowForm是一种通过为其分配MDIParent来显示frm形式的方法

结束功能))







它工作得很好
To access the controls that r created in the background-worker..use the invoke method of the controls-

below code is in DO_WORK() method of backgroundworker

''form_name is a string argument passed to RunWorkerAsync method
dim frm as form = activator.createinstance(form_name,form)

''here "me" is d MDIPARENT of d form "frm" which will be assgned in a method below

Me.Invoke(New Action(Function()
ShowForm(frm) ''ShowForm is a method that displays the form "frm" by assigning a MDIParent to it
End Function))



it worked perfectly fine


这篇关于跨线程操作无效:在BACKGROUNDWORKER&中实例化一个表单。尝试以其他形式使用它时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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