VB.net设置值到backgroundworker内部的标签 [英] VB.net setting values to labels inside a backgroundworker

查看:132
本文介绍了VB.net设置值到backgroundworker内部的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码可以工作,但是我对我所做的事情表示怀疑,因为我将CheckForIllegalCrossThreadCalls设置为false,我认为这会给我的背景工作人员带来一些副作用.这是我的示例代码:

My code works but I am in doubt of what I did because i set CheckForIllegalCrossThreadCalls to false which I think would give some side-effects to my backgroundworker. Here is my sample code:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False

End Sub

Private Sub go_Click(sender As Object, e As EventArgs) Handles go.Click

    Try
        If BackgroundWorker1.IsBusy <> True Then
            BackgroundWorker1.RunWorkerAsync()
            resetevent.Set()
        End If

    Catch ex As Exception

    End Try

End Sub


Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

    Do

    Label1.Text = x
Label2.Text = Label1.Text
Label3.Text = Label2.Tex
Label4.Text = Label3.Text
Label5.Text = Label4.Text

    x+=1
    Loop While (x < 100)


End Sub

Private Sub BackgroundWorker1_ProgressChanged(sender As System.Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    Try



    Catch ex As Exception

    End Try
End Sub

Private Sub BackgroundWorker1_Completed(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted

    Try

    Catch ex As Exception

    End Try
End Sub

是否可以在不将CheckForIllegalCrossThreadCalls设置为False的情况下为后台工作人员中的标签设置值?因为我在程序中遇到了一些错误,即使尚未达到计数器限制,循环也会突然停止.

Is there a way for me to set values to labels inside a backgroundworker without setting CheckForIllegalCrossThreadCalls to False? Because I have experienced some bugs with my program where the loop suddenly stops even if the counter limit has not been reached yet.

推荐答案

创建方法

  Private Sub setLabelTxt(ByVal text As String, ByVal lbl As Label)
    If lbl.InvokeRequired Then
        lbl.Invoke(New setLabelTxtInvoker(AddressOf setLabelTxt), text, lbl)
    Else
        lbl.Text = text
    End If
End Sub
Private Delegate Sub setLabelTxtInvoker(ByVal text As String, ByVal lbl As Label)

,然后在DoWork中调用setLabelTxt.

and call setLabelTxt in DoWork.

由于我现在有点忙,我将在后面添加参考说明. 我也遇到了您的问题,这对我有用.

I will add the explanation a bit later with references as I am a bit busy right now. I had your problem also and this worked for me.

" 从工作线程安全访问控件的方法是通过委派. 首先,您测试控件的InvokeRequired属性,该属性将告诉您是否可以安全地访问控件. InvokeRequired是Control类中为线程安全的少数几个成员之一,因此您可以在任何地方访问它.如果该属性为True,则需要调用才能访问控件,因为当前方法是在拥有控件的Handle的线程之外的线程上执行的.

"The way to safely access controls from worker threads is via delegation. First you test the InvokeRequired property of the control, which will tell you whether or not you can safely access the control. InvokeRequired is one of the few members of the Control class that is thread-safe, so you can access it anywhere. If the property is True then an invocation is required to access the control because the current method is executing on a thread other than the one that owns the control's Handle.

通过调用控件的Invoke或BeginInvoke方法执行调用.您创建一个委托,该委托是一个包含对方法的引用的对象.优良作法是引用当前方法.然后,您将该委托传递给Invoke或BeginInvoke方法.实质上,这将再次在拥有控件的Handle的线程上再次调用引用的方法."

The invocation is performed by calling the control's Invoke or BeginInvoke method. You create a delegate, which is an object that contains a reference to a method. It is good practice to make that a reference to the current method. You then pass that delegate to the Invoke or BeginInvoke method. That will essentially call the referenced method again, this time on the thread that owns the control's Handle."

来源: jmcilhinney 帖子从工作线程访问控件 http://www.vbforums.com/showthread.php?498387-Accessing-来自工作人员线程的控件

我也是菜鸟,所以我不能比他解释得更好

I can't explain better than him as I'm a noob also

这篇关于VB.net设置值到backgroundworker内部的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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