vb中嵌套循环中的多线程

时间:2017-08-31 11:51:29

标签: vb.net multithreading threadpool

我是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

这里我有一个名为copy的副本和一个sub' guiCopy'在里面 我每次都在循环中创建该obj的新实例。同样我想要一个新线程。

但我有一个问题,它说初始化Thread类的新实例。 对不起,我知道我的错,我不知道如何实施它。

只需要帮助

1 个答案:

答案 0 :(得分:0)

             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