vb 嵌套循环中的多线程
multiple threading in nested loop in vb
我是 VB .NET 中线程概念的新手,每个循环都有两个嵌套,每次循环执行时我都想要一个新线程。我在这里所做的是,在两个嵌套循环的帮助下,我一次又一次地使用不同的参数调用一个函数。我想要的是每次我调用作为新线程执行的函数时。
这是我的代码 -
Dim threadCount As Integer = 1
Dim theradArray(100) As System.Threading.Thread
Dim copyProcessID As Integer = 0
For Each dest_path As String In destList
If Directory.Exists(dest_path) Then
'copy process
Dim copyProcessOBJ As copy
For index = 0 To sourceList.Count - 1
source = sourceList(index)
If source.isChecked = True Then
copyProcessID += 1
If cliRadioButton.Checked Then
cmdCopy(source.Path, dest_path)
Else
ProgressReports.Show()
copyProcessOBJ = New copy
threadCount += 1
theradArray(threadCount) = New _System.Threading.Thread(AddressOf copyProcessOBJ.guiCopy)
theradArray(threadCount).Start()
copyProcessOBJ.guiCopy(source.Path, dest_path, copyProcessID)
End If
End If
Next
这里我有一个 class 命名的副本和一个子 'guiCopy' 在里面
我每次都在循环中创建该对象的新实例。同样,我想要一个新线程。
但是我有一个问题,它说初始化 Thread class 的新实例。
对不起,我知道我不知道如何实施它是我的错。
只需要帮助
Dim threadCount As Integer = 1
Dim theradArray(100) As System.Threading.Thread
Dim copyProcessID As Integer = 0
Dim threads As List(Of System.Threading.Thread) = New List(Of System.Threading.Thread)
For Each dest_path As String In destList
If Directory.Exists(dest_path) Then
Dim thr As System.Threading.Thread = New System.Threading.Thread(AddressOf yourfunction)
threads.Add(thr)
threads.LastOrDefault().Start()
End If
Next
Private Sub yourfunction()
Dim copyProcessOBJ As copy
For index = 0 To sourceList.Count - 1
source = sourceList(index)
If source.isChecked = True Then
copyProcessID += 1
If cliRadioButton.Checked Then
cmdCopy(source.Path, dest_path)
Else
ProgressReports.Show()
copyProcessOBJ = New copy
threadCount += 1
theradArray(threadCount) = New _System.Threading.Thread(AddressOf copyProcessOBJ.guiCopy)
theradArray(threadCount).Start()
copyProcessOBJ.guiCopy(source.Path, dest_path, copyProcessID)
End If
End If
End Sub
Next
我是 VB .NET 中线程概念的新手,每个循环都有两个嵌套,每次循环执行时我都想要一个新线程。我在这里所做的是,在两个嵌套循环的帮助下,我一次又一次地使用不同的参数调用一个函数。我想要的是每次我调用作为新线程执行的函数时。
这是我的代码 -
Dim threadCount As Integer = 1
Dim theradArray(100) As System.Threading.Thread
Dim copyProcessID As Integer = 0
For Each dest_path As String In destList
If Directory.Exists(dest_path) Then
'copy process
Dim copyProcessOBJ As copy
For index = 0 To sourceList.Count - 1
source = sourceList(index)
If source.isChecked = True Then
copyProcessID += 1
If cliRadioButton.Checked Then
cmdCopy(source.Path, dest_path)
Else
ProgressReports.Show()
copyProcessOBJ = New copy
threadCount += 1
theradArray(threadCount) = New _System.Threading.Thread(AddressOf copyProcessOBJ.guiCopy)
theradArray(threadCount).Start()
copyProcessOBJ.guiCopy(source.Path, dest_path, copyProcessID)
End If
End If
Next
这里我有一个 class 命名的副本和一个子 'guiCopy' 在里面 我每次都在循环中创建该对象的新实例。同样,我想要一个新线程。
但是我有一个问题,它说初始化 Thread class 的新实例。 对不起,我知道我不知道如何实施它是我的错。
只需要帮助
Dim threadCount As Integer = 1
Dim theradArray(100) As System.Threading.Thread
Dim copyProcessID As Integer = 0
Dim threads As List(Of System.Threading.Thread) = New List(Of System.Threading.Thread)
For Each dest_path As String In destList
If Directory.Exists(dest_path) Then
Dim thr As System.Threading.Thread = New System.Threading.Thread(AddressOf yourfunction)
threads.Add(thr)
threads.LastOrDefault().Start()
End If
Next
Private Sub yourfunction()
Dim copyProcessOBJ As copy
For index = 0 To sourceList.Count - 1
source = sourceList(index)
If source.isChecked = True Then
copyProcessID += 1
If cliRadioButton.Checked Then
cmdCopy(source.Path, dest_path)
Else
ProgressReports.Show()
copyProcessOBJ = New copy
threadCount += 1
theradArray(threadCount) = New _System.Threading.Thread(AddressOf copyProcessOBJ.guiCopy)
theradArray(threadCount).Start()
copyProcessOBJ.guiCopy(source.Path, dest_path, copyProcessID)
End If
End If
End Sub
Next