Threading parameters

koure

Active member
Joined
Sep 23, 2008
Messages
27
Programming Experience
Beginner
Hi

I'm newbie to vb.net
I'm writing a program which uses threads. the problem is that I'm trying to pass a parameter using a thread:

VB.NET:
Sub moo

Dim t As Thread
t = New Thread(AddressOf Me.foo(sName))
t.Start()

End Sub

Sub foo(ByVal sName as string)
.......
End Sub

When I'm trying to run the program I receive an error "'AddressOf' operand must be the name of a method; no parentheses needed" something like that.

Could anyone help me please because I do not know how to get over with this error

thanks
 
Thread.Start() uses ThreadStart delegate which is a sub method with no parameters, and Thread.Start(parameter) uses ParameterizedThreadStart delegate which is a sub that takes one parameter of type Object (a). Usually when managing threads yourself you have to pass information through properties of a class instance (b):
  • VB.NET:
    Sub Moo()
        Dim th As New Threading.Thread(AddressOf Foo)
        th.Start("hello world")
    End Sub
    
    Private Sub Foo(ByVal state As Object)
        MsgBox(CStr(state))
    End Sub
  • VB.NET:
    Sub Moo()
        Dim instance As New Too
        instance.NewProperty = "hello"
        Dim th As New Threading.Thread(AddressOf instance.Foo)
        th.Start()
    End Sub
    
    Class Too
    
        Private newPropertyValue As String
        Public Property NewProperty() As String
            Get
                Return newPropertyValue
            End Get
            Set(ByVal value As String)
                newPropertyValue = value
            End Set
        End Property
    
        Public Sub Foo()
            MsgBox(Me.newPropertyValue)
        End Sub
    End Class

When using the ThreadPool you have the same option for using a state Object parameter (a), you can also declare your own parameterized delegate (b).
  • VB.NET:
    Sub Moo()
        Threading.ThreadPool.QueueUserWorkItem(AddressOf Foo, "hello")
    End Sub
    
    Private Sub Foo(ByVal state As Object)
        MsgBox(CStr(state))
    End Sub
  • VB.NET:
    Private Delegate Sub WorkHandler(ByVal input1 As String, ByVal input2 As String)
    
    Sub Moo()
        Dim worker As New WorkHandler(AddressOf Foo)
        worker.BeginInvoke("hello", "world", AddressOf FooCallback, Nothing)
    End Sub
    
    Private Sub Foo(ByVal input1 As String, ByVal input2 As String)
        MsgBox(input1)
        MsgBox(input2)
    End Sub
    
    Private Sub FooCallback(ByVal ia As IAsyncResult)
        CType(CType(ia, Runtime.Remoting.Messaging.AsyncResult).AsyncDelegate, WorkHandler).EndInvoke(ia)
    End Sub
 
Hi
thanks for your reply

Sub Moo()
Dim th As New Threading.Thread(AddressOf Foo)
th.Start("hello world")
End Sub

Private Sub Foo(ByVal state As Object)
MsgBox(CStr(state))
End Sub

I think this code is pretty simple. I will use this one.
Do you suggest me adding anything like say ending the thread after its finished?

thanks
 
The thread ends when it is finished, when it has no more code lines to run.
 
Back
Top