C# version here.
Originally there was no way to pass data directly to a method that you were using as the entry point for a thread. You had to assign your data to one or more member variables and then retrieve it again in the new thread.
vb.net Code:
Private data As Integer
Private Sub InitiateThread()
Me.data = 100
Dim t As New Thread(AddressOf DoWork)
t.Start()
End Sub
Private Sub DoWork()
Dim data As Integer = Me.data
'Use data here.
End Sub
Many developers would create classes specifically for the new thread that incorporated the data, which was assigned to properties, and the thread entry point.
vb.net Code:
Private Class Worker
Private _data As Integer
Public WriteOnly Property Data() As Integer
Set(ByVal value As Integer)
Me._data = value
End Set
End Property
Public Sub DoWork()
Dim data As Integer = Me._data
'Use data here.
End Sub
End Class
Private Sub InitiateThread()
Dim w As New Worker
w.Data = 100
Dim t As New Thread(AddressOf w.DoWork)
t.Start()
End Sub
With .NET 2.0 came the ParameterizedThreadStart delegate and the ability to pass a single object to the entry method via the Thread.Start method.
vb.net Code:
Private Sub InitiateThread()
Dim t As New Thread(AddressOf DoWork)
t.Start(100)
End Sub
Private Sub DoWork(ByVal obj As Object)
Dim data As Integer = CInt(obj)
'Use data here.
End Sub
Now, with VB 2008, we have a new way, thanks to Lambda Expressions. This is easier and neater than the old ways and overcomes the weak typing required by the ParameterizedThreadStart delegate. With this new approach you can write a function that takes as many arguments as you like of whatever type you like. You then create a ThreadStart delegate using a Lambda Expression that calls this function, e.g.
vb.net Code:
Private Sub InitiateThread()
Dim t As New Thread(DirectCast(Function() DoWork(100), ThreadStart))
t.Start()
End Sub
Private Function DoWork(ByVal data As Integer) As Object
'Use data here.
Return Nothing
End Function
The only real drawback with this approach is that your work method MUST be a function, even though the return value cannot be used anywhere. When VB 2010 arrives that will no longer be the case because Lambda Expressions will be able to be Subs as well as Functions.
I have to admit that I don't really understand how a cast from a Lambda Expression that's a function to type ThreadStart, which has no return type, is legal but it seems to work so I'm happy to use it.