How to initialize the array as multi Thread

Mansour_Dalir 2,036 Reputation points
2023-05-31T21:15:50.75+00:00

Hi .

Did I do it right or wrong? Here I used the ListBox, but it is supposed to be used in the DataTable or DataGridView. What can be a faster way to parallelize processes?


Dim myArray(100) As String
    Dim bit1 As Boolean
    Dim bit2 As Boolean
    Dim bit3 As Boolean
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Do you think it is done in parallel?
             Dim thread1 As New Threading.Thread(AddressOf processing1)
          thread1.Start()
               Dim thread2 As New Threading.Thread(AddressOf processing2)
        thread2.Start()
               Dim thread3 As New Threading.Thread(AddressOf processing3)
         thread3.Start()
            While True
         'An infinite loop until the work of the processes is finished
            If (bit1 And bit2 And bit3) Then
                ListBox1.Items.AddRange(myArray)
                Exit While
            End If
        End While
End Sub
    Sub processing1()
     'From 0 to 30 To make Part of the array
        For a = 0 To 30 
             myArray(a) = a
        Next
          bit1 = True
    End Sub
    Sub processing2()
      'From 31 to 60 To make Part of the array
        For a = 31 To 60 
            myArray(a) = a
        Next
        bit2 = True
    End Sub
    Sub processing3()
         'From 60 to 100 To make Part of the array
        For a = 60 To 100
            myArray(a) = a
        Next
        bit3 = True
    End Sub
Developer technologies VB
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2023-06-01T07:44:13.2366667+00:00

    To create the threads automatically depending on environment, try this:

    Parallel.For(0, myArray.Length, Sub(i) myArray(i) = i.ToString)
    
    ListBox1.Items.AddRange(myArray)
    

    In case of other objects, make sure that the objects and operations are thread-safe.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.