Question Simple Question about Threading

JoeFX

Member
Joined
Mar 18, 2009
Messages
6
Programming Experience
3-5
Hi all,

I'm new to .NET and I'm trying to learn a bit about Threading...

I've got a simple example here which starts a new thread for 'DoTheWork'...

VB.NET:
Sub StartThread()
        Dim myThread As New Thread(AddressOf DoTheWork)
        myThread.Start(100)
End Sub

Sub DoTheWork(ByVal customerID As Integer)
        Do
            ' the work
        Loop
End Sub

If I change the "customerID" parameter from 'ByVal' to 'ByRef', I get a compile error...

"Overload resolution failed because no accessible New can be called with these arguements"

VB.NET:
Sub StartThread()
        Dim customerIDValue as integer = 200
        Dim myThread As New Thread(AddressOf DoTheWork)
        myThread.Start(customerIDValue)
End Sub

Sub DoTheWork([B]ByRef[/B] customerID As Integer)
        Do
            ' the work
        Loop
End Sub


How can I pass in a reference rather than a value when I start the new Thread?

:confused:
 
Please post in the most appropriate forum for the topic of your thread, not the first one you come to. Moved.

I'm afraid that what you're trying to do doesn't make sense. The only reason to declare a method parameter ByRef instead of ByVal is that you want to assign a new value to the parameter inside the method and have that change affect the original variable that you passed as an argument. Why would you want to do that in this case?

Even if you DID want to do that you couldn't in the case of a new thread. You're calling the Thread.Start method, not the DoTheWork method. Thread.Start doesn't have a ByRef parameter so the variable you pass in can't be changed by it. Even if your DoTheWork method could have a ByRef parameter it would simply change the value of the variable passed to it, which is several steps removed from the variable you pass to Thread.Start.
 
Back
Top