Threading won't work on a function with parameters

E

esource

Hi all,
I'm trying to use threading model in my web service but addressof does
not work with functions
I'm using VS 2005
my code:

Public Function Start() as Boolean
Dim MyThread As New Thread(AddressOf DoSomething)
MyThread.SetApartmentState(ApartmentState.STA)
MyThread.Start()
End Function

Private Function DoSomething(stuff as string) as string
msgbox stuff
End Function

error is:
Overload resolution failed because no accessible 'New' can be called
with these arguments:
'Public Sub New(start As
System.Threading.ParameterizedThreadStart)': Method 'Private Function
DoSomething(stuff As String) As String' does not have the same
signature as delegate 'Delegate Sub ParameterizedThreadStart(obj As
Object)'.
'Public Sub New(start As System.Threading.ThreadStart)': Method
'Private Function DoSomething(stuff As String) As String' does not
have the same signature as delegate 'Delegate Sub ThreadStart()'.

Why is it trying to compare against the delegate
ParameterizedThreadStart?
If I do it this way it works fine:

Public Function Start() as Boolean
Dim MyThread As New Thread(AddressOf DoSomething)
MyThread.SetApartmentState(ApartmentState.STA)
MyThread.Start()
End Function

Private Sub DoSomething(stuff as object)
dim x as new object
x = stuff
End Sub

but this does not help me out. I need a function not a procedure
any help appreciated. thanks
 
G

Guest

Why is it trying to compare against the delegate
ParameterizedThreadStart?
If I do it this way it works fine:

Threads cannot return parameters... you'll need to return the value through
a property or some other method.
 
A

Armin Zingler

esource said:
Public Function Start() as Boolean
Dim MyThread As New Thread(AddressOf DoSomething)
MyThread.SetApartmentState(ApartmentState.STA)
MyThread.Start()
End Function

Private Function DoSomething(stuff as string) as string
msgbox stuff
End Function

error is:
Overload resolution failed because no accessible 'New' can be called
with these arguments:
'Public Sub New(start As
System.Threading.ParameterizedThreadStart)': Method 'Private
Function DoSomething(stuff As String) As String' does not have the
same signature as delegate 'Delegate Sub
ParameterizedThreadStart(obj As Object)'.
'Public Sub New(start As System.Threading.ThreadStart)': Method
'Private Function DoSomething(stuff As String) As String' does not
have the same signature as delegate 'Delegate Sub ThreadStart()'.

Why is it trying to compare against the delegate
ParameterizedThreadStart?

The Thread's overloaded constructor only accepts 1 argument that must be of
type ParameterizedThreadStart.

The delegate signatures don't match. Your target procedure only accepts
strings while the Delegate is declared as

Public Delegate Sub ParameterizedThreadStart(ByVal obj As Object)

which accepts every object, like in your 2nd example.


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top