Why does thread.join freezes the UI in vb.net

I am trying my hand on multi threading. After creating the threads my UI seems to freeze as i am trying to join the created threads. If i dont join my threads then everything seems to work.

Below is my code.

Public Class FrmGraphWithThreads

    Dim t As Thread
    Dim tlist As List(Of Thread)

    Private Sub FrmGraphWithThreads_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        UpdateTextDelegate = New UpdateTextSub(AddressOf UpdateText)
        tlist = New List(Of Thread)
    End Sub


#Region " Delegate and Delegate SUB to update Text Box "

    Private Delegate Sub UpdateTextSub(ByRef txtbx As TextBox, ByVal val As String)
    Private UpdateTextDelegate As UpdateTextSub
    Private Sub UpdateText(ByRef txtbx As TextBox, ByVal val As String)
        txtbx.Text = val
    End Sub

#End Region

    Private Sub CalculateTable(ByVal itm As Object)
        Dim _int = CInt(itm)
        Dim i As Integer

        If _int > 0 Then
            Select Case _int
                Case 1
                    For i = 1 To 10
                        Dim _val = _int * i
                        txtBx1.Invoke(UpdateTextDelegate, New Object() {txtBx1, _val.ToString})
                        Thread.Sleep(1000)
                    Next
                Case 2
                    For i = 1 To 10
                        Dim _val = _int * i
                        txtBx2.Invoke(UpdateTextDelegate, New Object() {txtBx2, _val.ToString})
                        Thread.Sleep(1000)
                    Next
            End Select
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim i As Integer

        For i = 1 To 2
            t = New Thread(AddressOf CalculateTable)
            t.Name = i.ToString
            t.Start(i)
            tlist.Add(t)
            Thread.Sleep(10)
        Next

        For Each itm As Thread In tlist
            itm.Join()'This causes freezing. Without join everything is working.
        Next
    End Sub
End Class

Any ideas would be of great help.

Jon Skeet
people
quotationmark

You're calling Thread.Join from the UI thread. That means the UI thread is blocked until all the other threads complete. That's a bad idea in general as it means your UI is blocked while all the other threads do work.

In this case it's worse than a bad idea though - it will deadlock, because you're calling Control.Invoke from the other threads, e.g.:

txtBx2.Invoke(UpdateTextDelegate, New Object() {txtBx2, _val.ToString})

Control.Invoke blocks the calling thread until the UI has executed the given delegate. So you have background threads that are blocked until the UI thread has finished work, and the UI thread is blocked until the background threads have finished. Deadlock.

You should almost certainly be looking into using async/await and tasks instead of this manual threading. It'll make your life a lot easier.

people

See more on this question at Stackoverflow