BackgroundWorker跨线程操作无效 [英] BackgroundWorker Cross-Thread operation not valid

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

问题描述

早上好



我有一个vb.net程序启动另一个表格。



这个form使用BackgroundWorker构建PDF,同时在表单上显示progess栏。



 私人  Sub  frm_BuildPDF_Load( ByVal  sender 正如系统。对象 ByVal  e  As  System.EventArgs)句柄  MyBase  .Load 

ProgressBar1。最小= 0
ProgressBar1.Maximum = 1000
ProgressBar1.Value = 999

BackgroundWorker1.RunWorkerAsync()

结束 < span class =code-keyword> Sub


私有 Sub BackgroundWorker1_DoWork( ByVal 发​​件人作为系统。对象 ByVal e As System.ComponentModel.DoWorkEventArgs)句柄 BackgroundWorker1.DoWork
InitiateBuild()
.Close()
结束 < span class =code-keyword> Sub





程序在以下消息时失败它击中了Me.Close声明:



System.InvalidOperationException未被用户代码处理

HResult = -2146233079

消息=跨线程操作无效:控制从其创建的线程以外的线程访问的'frm_BuildPDF'。

Source = System.Windows.Forms

StackTrace:

在System.Windows.Forms.Control.get_Handle()

在System。 Windows.Forms.Control.SendMessage(Int32 msg,Int32 wparam,Int32 lparam)

在System.Windows.Forms.Form.Close()

在PurchaseOrder.frm_BuildPDF。 BackgroundWorker1_DoWork(Object sender,DoWorkEventArgs e)位于C:\ Users \Darrelld \Documents \ Source Code \ Power Systems \PurchaseOrder\PurchaseOrder\frm_BuildPDF.vb:第45行
在System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)

at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)

InnerException:



之前我从未使用过BackgroundWorker,我所做的所有阅读都没有多大意义,或者看起来过于复杂。



所有我我试图实现的是向用户显示程序正忙。

当表格完成后,它必须简单地关闭。



请问我如何关闭表格。

请注意,用户无需进行任何交互。



谢谢大家

解决方案

您无法访问控件或任何其他UI组件,包括除了创建它们之外的任何线程的表单:UI线程。



如果你想要要访问BackgroundWorker中的控件,您必须调用它们: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invoke(v = vs.110).aspx [ ^ ]

这会将代码移动到正确的线程上并继续执行。请注意,如果后台工作程序的想法是在执行大量更新时释放UI线程,则它将不会产生预期的效果:因为Invoke会将执行移回UI线程,所以它可能会减慢速度!

Good morning

I have a vb.net program that launches another form.

This form uses the BackgroundWorker to build a PDF whilst a progess bar is shown on the form.

Private Sub frm_BuildPDF_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    ProgressBar1.Minimum = 0
    ProgressBar1.Maximum = 1000
    ProgressBar1.Value = 999

    BackgroundWorker1.RunWorkerAsync()

End Sub


Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    InitiateBuild()
    Me.Close()
End Sub



The program falls over with the following message when it hits the Me.Close statement :

System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=Cross-thread operation not valid: Control 'frm_BuildPDF' accessed from a thread other than the thread it was created on.
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.SendMessage(Int32 msg, Int32 wparam, Int32 lparam)
at System.Windows.Forms.Form.Close()
at PurchaseOrder.frm_BuildPDF.BackgroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in C:\Users\Darrelld\Documents\Source Code\Power Systems\PurchaseOrder\PurchaseOrder\frm_BuildPDF.vb:line 45
at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
InnerException:

I have never used the BackgroundWorker before and all the reading I have done is not making much sense or seems over complicated.

All I am trying to achieve is show the user that the program is busy.
When the form is complete it must simply close.

Could any please advise how I close the form.
Note that there is no interaction required with the user.

Thanks everyone

解决方案

You cannot access controls or any other UI components, including forms from any thread except the one they were created on: the UI thread.

If you want to access controls in a BackgroundWorker, you have to Invoke them: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invoke(v=vs.110).aspx[^]
This "moves the code" onto the correct thread and continues execution from there. Do note that if the idea of the background worker is to free up the UI thread while doing a lot of updates, it won't have the desired effect: since the Invoke will move execution back onto the UI thread it may well slow things down!


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

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