Define Thread

StewartBW 1,805 Reputation points
2024-05-10T15:08:53.2166667+00:00

Hello,

In this code:

Dim ThreadBlah As New Thread(AddressOf BlahThread)
vs
Dim ThreadBlah As New Thread(New ParameterizedThreadStart(AddressOf BlahThread))
ThreadBlah.Start
Private Sub BlahThread(ByVal InParam As Object)
...
End Sub

I can define ThreadBlah in 2 ways, both work:

Dim ThreadBlah As New Thread(AddressOf BlahThread)
vs
Dim ThreadBlah As New Thread(New ParameterizedThreadStart(AddressOf BlahThread))

Are both valid? The first one works although it does not state the Parameterized.

Thanks.

Developer technologies VB
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2024-05-10T15:24:15.22+00:00

    If the Visual Studio is properly configured for step-by-step debugging, you will see that both of New Thread execute the same constructor that takes a ParameterizedThreadStart. Therefore the statements are equivalent. The compiler selects the appropriate constructor in both cases.

    However, ThreadBlah.Start(...) should include an argument that will be passed to InParam.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2024-05-10T15:38:20.9933333+00:00

    the .net automatic type casting is why the second works. the Thread constructor has two delegates:

    • void method with no parameters
    • void method with one object parameter

    if you pass the address of a method, type casting determine which delegate overload to use.

    0 comments No comments

  2. Dewayne Basnett 1,381 Reputation points
    2024-05-10T18:09:31.14+00:00

    Here is another approach to this using Tasks instead of Threads...

    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        BlahTask = Task.Run(Sub()
    
                                BlahActionString("TEST")
    
                            End Sub)
    
        Await BlahTask
    
        BlahTask = Task.Run(Sub()
    
                                BlahActionInteger(42)
    
                            End Sub)
    
        Await BlahTask
    
    End Sub
    
    Dim BlahTask As Task
    
    Dim BlahActionString As Action(Of String) = Sub(S As String)
    
                                                    BlahThread(S)
    
                                                End Sub
    
    Dim BlahActionInteger As Action(Of Integer) = Sub(I As Integer)
    
                                                      BlahThread(I)
    
                                                  End Sub
    
    Private Sub BlahThread(ByVal InParam As Object)
    
        ' blah, blah
    
        Debug.WriteLine(InParam.ToString)
    
    End Sub
    

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.