Define Thread

StewartBW 565 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.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,417 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,612 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 113.4K 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) 58,206 Reputation points
    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,361 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