2017-08-31 53 views
0

我是新的线程在VB.NET中的概念,我有两个嵌套每个循环,我想每个循环执行一个新的线程。我在这里做的是,我在两个嵌套循环的帮助下,每次使用不同的参数调用一个函数。我想要的是每次我调用该函数作为新线程执行时。在嵌套循环中的多线程在vb

这里是我的代码 -

  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 

在这里,我有一个名为副本类和子“guiCopy”里面那个 我正在做的新实例OBJ在循环每次。同样,我想要一个新的线程。

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

只是需要帮助

+1

请包括完整的异常信息。另外,copy.guiCopy方法实际上做了什么?你开始多少个线程等等,因为我确定这是一个糟糕的设计,你的问题可以用更简单的方法解决,可能使用任务或parallel.for循环 – Steve

回答

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