如何启动 Backgroundworker ByRef
How to start a Backgroundworker ByRef
我想为后台工作人员指定一个自定义名称,所以我创建了一个自定义名称 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
我有这些 NamedBKW 的列表,我想通过循环迭代来一次启动一个。该列表是在程序启动时构建的。
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
但由于某种原因,这不起作用。 console/log 打印它正在启动每个线程的消息,但线程实际上并未启动。我做错了什么?
所有 Backgroundworker 都在主 class 中声明,如下所示:
Friend WithEvents BackupStage1BKW As New BackgroundWorker
您有这样的声明来创建后台工作对象:
Friend WithEvents BackupStage1BKW As New BackgroundWorker
但是,您所展示的内容中没有任何内容表明这些工人实际上做了任何事情。如果有一种方法可以为作为 WithEvents
声明的一部分连接的每个工作人员处理 DoWork
事件,我们看不到它。
我暂时假设这些方法确实存在并且正确地订阅了事件。在这种情况下,我们进入下一期。此行覆盖声明的对象:
thisBKW = New BackgroundWorker
您现在拥有 全新的对象,之前存在的任何事件处理程序都不再适用。
您确实不想要那条线,并且您不想要 ByRef
这里。请记住,ByVal 仍然传递引用! 唯一的区别是它传递了一个 copy 的引用,事实证明这正是您最需要的时间。
我想为后台工作人员指定一个自定义名称,所以我创建了一个自定义名称 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
我有这些 NamedBKW 的列表,我想通过循环迭代来一次启动一个。该列表是在程序启动时构建的。
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
但由于某种原因,这不起作用。 console/log 打印它正在启动每个线程的消息,但线程实际上并未启动。我做错了什么?
所有 Backgroundworker 都在主 class 中声明,如下所示:
Friend WithEvents BackupStage1BKW As New BackgroundWorker
您有这样的声明来创建后台工作对象:
Friend WithEvents BackupStage1BKW As New BackgroundWorker
但是,您所展示的内容中没有任何内容表明这些工人实际上做了任何事情。如果有一种方法可以为作为 WithEvents
声明的一部分连接的每个工作人员处理 DoWork
事件,我们看不到它。
我暂时假设这些方法确实存在并且正确地订阅了事件。在这种情况下,我们进入下一期。此行覆盖声明的对象:
thisBKW = New BackgroundWorker
您现在拥有 全新的对象,之前存在的任何事件处理程序都不再适用。
您确实不想要那条线,并且您不想要 ByRef
这里。请记住,ByVal 仍然传递引用! 唯一的区别是它传递了一个 copy 的引用,事实证明这正是您最需要的时间。