后台工作者问题 [英] Background worker problem

查看:80
本文介绍了后台工作者问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在测试后台工人.我正在运行以下代码进行测试.

Hi,
I am testing a background workder. I am running the following code for the test.

Private Sub bgwTest_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwTest.DoWork
       Dim a As Integer = 0
       Do While a < 10 'Infinite loop
           ComboBox1.Items.Add(1)
       Loop
   End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       PictureBox1.Visible = True 'Contains my animated GIF
'Purpose is GIF keeps looping (it is animated GIF) despite computer is stuck in infinite loop
       bgwTest.RunWorkerAsync()
   End Sub






但是这段代码会产生以下错误:

跨线程操作无效:从创建该线程的线程以外的线程访问控件"ComboBox1"."

请帮忙.
谢谢
Furqan






But this code generates the following error:

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

Please help.
Thanks
Furqan

推荐答案

使用Delegate和InvokeRequired可以将项目添加到ComboBox中,就像这样

Use a Delegate and InvokeRequired to add items to the ComboBox like this

Delegate Sub SetComboCallBack(ByVal i As Integer)


Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim a As Integer = 0
        Do While a < 10 'Infinite loop
            UpdateComboBox(a)
            a += 1
        Loop

    End Sub
    Private Sub UpdateComboBox(ByVal i As Integer)
        If Me.ComboBox1.InvokeRequired Then
            Dim d As New SetComboCallBack(AddressOf UpdateComboBox)
            Me.Invoke(d, New Object() {i})
        Else
            Me.ComboBox1.Items.Add(i)
        End If
    End Sub



希望这对您有帮助



Hope this helps


您已经拥有的答案还不是100%完整.
您无法从非UI线程调用与UI相关的任何操作.您需要使用System.Threading.DispatcherInvokeBeginInvoke方法(适用于Forms和WPF),或使用System.Windows.Forms.Control(仅适用于Forms).

Invoke不同,BegingInvoke立即返回.现在,如果您确定总是从非UI线程调用调用方法,则可能不使用InvokeRequired,因为InvokeRequired将始终返回true.使用哪个控件来调用InvokeBeginInvoke都无关紧要;它仅应是参与当前运行的Application的控件.

请查看我过去的答案,以获取有关其工作原理和用法示例的详细说明:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
The answers you already have are not 100% complete.
You cannot call anything related to UI from a non UI thread. You need to use either Invoke or BeginInvoke methods of System.Threading.Dispatcher (works with both Forms and WPF), or System.Windows.Forms.Control (Forms only).

Unlike Invoke, BegingInvoke returns immediately. Now, you may not use InvokeRequired if you''re sure that invocation method is called always from a non-UI thread because InvokeRequired will always return true. It does not matter which control is used to call Invoke or BeginInvoke; it only should be the control participating in currently running Application.

Please see my past answers for detailed explanation on how it works and usage samples:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA




看这篇文章:

如何解决跨线程操作无效" [需要有关跨线程操作的帮助C#线程 [ ^ ]

瓦莱里.


Look at this article:

How to solve "Cross thread operation not valid"[^]

or this question...

Need a help for cross thread operation in C# Threads[^]

Valery.


这篇关于后台工作者问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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