0

Hello I am studying multithreading with join method

I am trying to create multiple threads and run thread in order.

I know that join method is able to join threads and run threads in order.

However, form is updating text box, but whole form is freezing so i can't click any button in the form

Here is code that I have..

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
    Dim T_List As New ArrayList
    Dim VALE As Integer = 99
    For i = 0 To 2
        VALE = i
        t = New Thread(New ThreadStart(Sub() run_t(VALE, 10 + (5 * VALE))))
        t.IsBackground = True
        t.Start()
        t.Join()
        ' T_List.Add(t)
    Next

End Sub


Private Sub run_t(t_num As Integer, number As Integer)
    For i = 0 To number
        Thread.Sleep(100)
        CounterBox.AppendText("thread : " & t_num & " : " & i.ToString() + vbNewLine)
        If i Mod 2 = 0 Then
            ComboBox1.Items.Add(i)
        End If

    Next
End Sub

I was checking "STATHREAD" STATHREAD and main thread and https://msdn.microsoft.com/en-us/library/ms182351.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

however, STATHREAD seems not working for this case.

what can be the solution to use Join method for threads without freezing form..

philipxy
  • 14,867
  • 6
  • 39
  • 83
NBB
  • 137
  • 2
  • 6
  • 14

2 Answers2

0

In VB.Net Form, I would recommand you to use BackgroundWorker which will let you to:

  • launch a calculation thread
  • interact with your Form without blocking the UI thread.

BackgroundWorker

Thread.Join is a blocking method which should never be used in GUI event handlers.

Jeandey Boris
  • 743
  • 4
  • 9
0
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
    Dim tt As New Thread(AddressOf working) : tt.Start()
End Sub

Sub working()
    Dim T_List As New ArrayList
    Dim VALE As Integer = 99
    For i = 0 To 2
        VALE = i
        run_t(VALE, 10 + (5 * VALE))
        
' T_List.Add(t)
    Next
End Sub

Sub run_t(t_num As Integer, number As Integer)
    For i = 0 To number
        Thread.Sleep(100)
        CounterBox.AppendText("thread : " & t_num & " : " & i.ToString() + vbNewLine)
        If i Mod 2 = 0 Then
            ComboBox1.Items.Add(i)
        End If
        
    Next
End Sub
gurkan
  • 884
  • 4
  • 16
  • 25
zdlk
  • 21
  • 4