开发者

VB.NET 2008 background worker

开发者 https://www.devze.com 2023-02-24 06:40 出处:网络
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

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开发者_StackOverflow中文版

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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消