Results 1 to 7 of 7

Thread: Passing Data to a Thread Entry Method

Threaded View

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Passing Data to a Thread Entry Method

    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:
    1. Private data As Integer
    2.  
    3. Private Sub InitiateThread()
    4.     Me.data = 100
    5.  
    6.     Dim t As New Thread(AddressOf DoWork)
    7.  
    8.     t.Start()
    9. End Sub
    10.  
    11. Private Sub DoWork()
    12.     Dim data As Integer = Me.data
    13.  
    14.     'Use data here.
    15. 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:
    1. Private Class Worker
    2.  
    3.     Private _data As Integer
    4.  
    5.     Public WriteOnly Property Data() As Integer
    6.         Set(ByVal value As Integer)
    7.             Me._data = value
    8.         End Set
    9.     End Property
    10.  
    11.     Public Sub DoWork()
    12.         Dim data As Integer = Me._data
    13.  
    14.         'Use data here.
    15.     End Sub
    16.  
    17. End Class
    18.  
    19.  
    20. Private Sub InitiateThread()
    21.     Dim w As New Worker
    22.  
    23.     w.Data = 100
    24.  
    25.     Dim t As New Thread(AddressOf w.DoWork)
    26.  
    27.     t.Start()
    28. 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:
    1. Private Sub InitiateThread()
    2.     Dim t As New Thread(AddressOf DoWork)
    3.  
    4.     t.Start(100)
    5. End Sub
    6.  
    7. Private Sub DoWork(ByVal obj As Object)
    8.     Dim data As Integer = CInt(obj)
    9.  
    10.     'Use data here.
    11. 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:
    1. Private Sub InitiateThread()
    2.     Dim t As New Thread(DirectCast(Function() DoWork(100), ThreadStart))
    3.  
    4.     t.Start()
    5. End Sub
    6.  
    7. Private Function DoWork(ByVal data As Integer) As Object
    8.     'Use data here.
    9.  
    10.     Return Nothing
    11. 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.
    Last edited by jmcilhinney; Dec 28th, 2008 at 11:16 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width