VB.NET 2008 后台工作者

发布于 2024-11-01 00:24:45 字数 2219 浏览 0 评论 0原文

我使用教程创建了一个后台工作者。我了解一些基本的东西,例如如何从其中更新标签或进度条。这个后台工作人员的目的是设置一堆变量,这些变量稍后将被其他几个后台工作人员使用。我设置的变量是 6 个不同的列表和一个多维数组。目前后台工作人员的代码如下所示。

Private Sub My_BgWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles My_BgWorker.DoWork
    For i As Integer = 0 To m_CountTo
        ' Has the background worker be told to stop?
        If My_BgWorker.CancellationPending Then
            ' Set Cancel to True
            'e.Cancel = True
            Exit For
        End If
        System.Threading.Thread.Sleep(1000) ' Sleep for 1 Second
        ' Report The progress of the Background Worker.
        My_BgWorker.ReportProgress(CInt((i / m_CountTo) * 100))
        SetLabelText_ThreadSafe(Me.lbl_Status, FormatPercent(i / m_CountTo, 2))
    Next
End Sub

setlabeltext 委托看起来像这样

' The delegate
Delegate Sub SetLabelText_Delegate(ByVal [Label] As Object, ByVal [text] As String)

' The delegates subroutine.
Private Sub SetLabelText_ThreadSafe(ByVal [Label] As Object, ByVal [text] As String)
    ' InvokeRequired required compares the thread ID of the calling thread to the thread ID of the creating thread.
    ' If these threads are different, it returns true.
    If [Label].InvokeRequired Then
        Dim MyDelegate As New SetLabelText_Delegate(AddressOf SetLabelText_ThreadSafe)
        Me.Invoke(MyDelegate, New Object() {[Label], [text]})
    Else
        [Label].Text = [text]
    End If
End Sub

这一切都很好,但我在尝试找出一种简单的方法来设置我的列表时迷失了。我设置其中一个列表的代码如下所示,

Public Class Person
    Public height As Integer
    Public weight As Integer
    Public age As Integer
    Public sex As Integer
    Public Sub New(ByVal height As Integer, ByVal weight As Integer, ByVal age As Integer, ByVal sex As String)
        Me.height = height
        Me.weight = weight
        Me.age = age
        Me.sex = sex
    End Sub
End Class

Persons.Add(new person(180,210,28,"male"))

我通常会使用 persons.add 向列表中添加一些内容,但我很难用一种简单的方法来解决问题后台工作人员将新值添加到几个不同的列表(这些列表的数据值数量会有所不同)。有没有简单的方法可以做到这一点?我习惯于仅在子顶部调用来执行此操作,但这些似乎不适用于 dowork 子内部。

另外,我的另一个问题是,一旦我开始工作并且设置了列表,从其他后台工作人员内部的这些列表中提取数据的最简单方法是什么?任何帮助都会很棒,谢谢!

I have created a background worker using a tutorial. I understand some basic stuff like how to update a label or a progress bar from inside of it. The purpose of this background worker is setup a bunch of variables that are going to be used by several other background workers later. The variables i am setting up are 6 different lists and a multi-dimensional array. The code looks like this for the background worker at the moment.

Private Sub My_BgWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles My_BgWorker.DoWork
    For i As Integer = 0 To m_CountTo
        ' Has the background worker be told to stop?
        If My_BgWorker.CancellationPending Then
            ' Set Cancel to True
            'e.Cancel = True
            Exit For
        End If
        System.Threading.Thread.Sleep(1000) ' Sleep for 1 Second
        ' Report The progress of the Background Worker.
        My_BgWorker.ReportProgress(CInt((i / m_CountTo) * 100))
        SetLabelText_ThreadSafe(Me.lbl_Status, FormatPercent(i / m_CountTo, 2))
    Next
End Sub

The setlabeltext delegate looks like this

' The delegate
Delegate Sub SetLabelText_Delegate(ByVal [Label] As Object, ByVal [text] As String)

' The delegates subroutine.
Private Sub SetLabelText_ThreadSafe(ByVal [Label] As Object, ByVal [text] As String)
    ' InvokeRequired required compares the thread ID of the calling thread to the thread ID of the creating thread.
    ' If these threads are different, it returns true.
    If [Label].InvokeRequired Then
        Dim MyDelegate As New SetLabelText_Delegate(AddressOf SetLabelText_ThreadSafe)
        Me.Invoke(MyDelegate, New Object() {[Label], [text]})
    Else
        [Label].Text = [text]
    End If
End Sub

This all works fine but I'm lost trying to figure out an easy way to setup my lists. My code to setup one of the lists looks like this

Public Class Person
    Public height As Integer
    Public weight As Integer
    Public age As Integer
    Public sex As Integer
    Public Sub New(ByVal height As Integer, ByVal weight As Integer, ByVal age As Integer, ByVal sex As String)
        Me.height = height
        Me.weight = weight
        Me.age = age
        Me.sex = sex
    End Sub
End Class

Persons.Add(new person(180,210,28,"male"))

I would normally use the persons.add to add something to the list, but I'm having a really hard time wrapping my brain around an easy way inside of the backgroundworker to add new values to several different lists(which are going to vary in amount of data values). Is there an easy way to do this? I'm used to just having invokes at the top of a sub to do this but those don't seem to work inside of a dowork sub.

Also my other question is once I get this working and I've got my lists set up, what is going to be easiest way to pull data from those lists inside of other background workers? Any help would be great, thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆光飞翔i 2024-11-08 00:24:45

Backgroundworker 主要用于更新 ui 而不使其冻结。

主线程的工作是将变量传递给其他后台工作线程。您可以使用 runworkercompleted 等待填充完成并访问列表。如果您想在填充列表时访问数据,请使用报告进度事件来读取部分填充的列表并将其传递给其他工作线程。

Backgroundworker is mainly used to update ui without making it freeze.

It's the job of the main thread to pass the variables around to other background worker threads. You can use the runworkercompleted to wait for the populating to complete and access the list. If you want to access the data while the lists are getting populated use the report progress event to read the partially populated list and pass it to other worker threads.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文