我有一个项目,我正在尝试设置多个背景工作者。
[旧守则] -backgroundworker1作为表单上的对象
sub run()
'backgroundworker1.runworkerasync()
end sub
sub do-work
'Run process calling sub "CopyDirectory"
end sub
- 这一切都很好。
我已将backgroundworker更改为以下代码
Dim NewWorker As New BackgroundWorker
NewWorker.WorkerReportsProgress = True
NewWorker.WorkerSupportsCancellation = True
AddHandler NewWorker.DoWork, AddressOf BackgroundWorker1_DoWork
AddHandler NewWorker.ProgressChanged, AddressOf_ BackgroundWorker1_ProgressChanged ' Old worker sub but still works
AddHandler NewWorker.RunWorkerCompleted, AddressOf_ BackgroundWorker1_RunWorkerCompleted' Old worker sub but still works
If NewWorker.IsBusy <> True Then
NewWorker.RunWorkerAsync()
End If
当我&#34;做工作&#34;
Private Sub Work2(ByVal CompNumber As Integer, ByVal Destination As String, ByVal Worker As BackgroundWorker)
' I can see the step happen to here
CopyDirectory(Source, Destination, "Computer " + CompNumber, Worker)
' I never can stepthrough to here
AppendTextBox(rtbStatusLog, Now() + " ?" + " Completed?" + vbCrLf)
End Sub
我可以单步执行代码,看看我的评论的重点,但是一旦我迈出了&#34; copydirectory&#34; sub被称为,步进结束,它就像&#34;工人完成&#34;
Private Sub CopyDirectory(ByVal sourcePath As String, ByVal destinationPath As String, ByVal TargetName As String, ByVal Worker As BackgroundWorker)
Dim SourceDirectoryInfo As New System.IO.DirectoryInfo(sourcePath)
' If the destination folder don't exist then create it
If Not System.IO.Directory.Exists(destinationPath) Then
System.IO.Directory.CreateDirectory(destinationPath)
End If
Dim fileSystemInfo As System.IO.FileSystemInfo
For Each fileSystemInfo In SourceDirectoryInfo.GetFileSystemInfos
Dim destinationFileName As String = System.IO.Path.Combine(destinationPath, fileSystemInfo.Name)
' Now check whether its a file or a folder and take action accordingly
If TypeOf fileSystemInfo Is System.IO.FileInfo Then
System.IO.File.Copy(fileSystemInfo.FullName, destinationFileName, True)
Worker.ReportProgress(0)
AppendTextBox(rtbStatusLog, Now() + " ?" + TargetName + ":? " + "File Copied: " + destinationFileName + vbCrLf)
Else
' Recursively call the mothod to copy all the neste folders
CopyDirectory(fileSystemInfo.FullName, destinationFileName, TargetName, Worker)
End If
Next
End Sub
我试图在copydirectory sub中休息,但它永远不会被击中。
我觉得这与线程有关,以及我是如何创建背景工作者的,但我不确定我的错误。