threading won't work on 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
 
C

coolCoder

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

Hi,
You can create a function such that instead of returning
something, it just sets the member variable of the class. So, if you
implement this, you will have to check the member variable value after
the thread has completed its execution.
Hope I am clear enough.
 
A

Aidy

You'll need to convert this to vb.net, but this is it in c#

protected void Page_Load(object sender, EventArgs e)
{
Thread t = new System.Threading.Thread(new
ParameterizedThreadStart(DoSomething));
t.Start("Hello");
}
public void DoSomething(object param)
{
string data = (string)param;
System.Diagnostics.Debug.WriteLine(data);
}
 

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

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top