How to start a Backgroundworker ByRef

49 Views Asked by At

I wanted to assign a custom name to a backgroundworker so I created a custom class:

 Public Class NamedBKW
        Public BKW As BackgroundWorker
        Public Name As String
        Public Sub New(ByVal thisBKW As BackgroundWorker, ByVal thisName As String)
            BKW = thisBKW
            Name = thisName
        End Sub
    End Class

I have a list of these NamedBKWs that I'd like to start one at a time by iterating through a loop. The list is built when the program starts.

Friend Sub StartService()
[...]

BuildLists()

For x As Integer = 0 To ListOfBKWs.Count - 1
                StartThisThread(ListOfBKWs(x).BKW, ListOfBKWs(x).Name)
                Thread.Sleep(3000)
            Next
End Sub

Public Sub BuildLists()
[...]
 ' List of BKWs:
            ' Note that the order listed here will determine the order they are started.
            ListOfBKWs = New List(Of NamedBKW)
            ListOfBKWs.Add(New NamedBKW(ScheduleCheckerBKW, "ScheduleChecker"))
            ListOfBKWs.Add(New NamedBKW(BackupStage5BKW, "Stage 5"))
            ListOfBKWs.Add(New NamedBKW(BackupStage4BKW, "Stage 4"))
            ListOfBKWs.Add(New NamedBKW(BackupStage3BKW, "Stage 3"))
            ListOfBKWs.Add(New NamedBKW(BackupStage2BKW, "Stage 2"))
            ListOfBKWs.Add(New NamedBKW(BackupStage1BKW, "Stage 1"))
            ListOfBKWs.Add(New NamedBKW(DeleteOldFilesBKW, "DeleteOldFiles"))
            ListOfBKWs.Add(New NamedBKW(FixerBKW, "Fixer"))
            ListOfBKWs.Add(New NamedBKW(ThreadCheckerBKW, "ThreadChecker"))
End Sub

Public Sub StartThisThread(ByRef thisBKW As BackgroundWorker, ByVal thisName As String)
            NPC.WriteToConsoleAndLog("MAIN: Starting " & thisName & " thread...")
            thisBKW = New BackgroundWorker
            thisBKW.WorkerReportsProgress = True
            thisBKW.WorkerSupportsCancellation = True
            thisBKW.RunWorkerAsync()
End Sub

But for some reason this does not work. The console/log prints the message that it is starting each thread, but the threads don't actually start. What am I doing wrong?

All of the Backgroundworkers are declared in the main class, like this:

Friend WithEvents BackupStage1BKW As New BackgroundWorker

1

There are 1 best solutions below

2
On BEST ANSWER

You have declarations to create the background worker objects like this:

Friend WithEvents BackupStage1BKW As New BackgroundWorker

However, nothing in what you've shown indicates these workers actually do anything. If there is a method to handle the DoWork event for each worker that is wired up as part of the WithEvents declaration, we can't see it.

I'll pretend for a moment these methods do exist and are correctly subscribed to the events. In that case we come to the next issue. This line over-writes the declared objects:

 thisBKW = New BackgroundWorker

You now have brand new objects, and any event handler that was there before no longer applies.

You really don't want that line, and you don't want ByRef here. Remember, ByVal still passes references! The only difference is it passes a copy of the reference, and it turns out that's exactly what you need most of the time.