469,306 Members | 1,916 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

Passing parameters to a procedure called in thread

How can I pass parameters to a procedure which is called in thread?

'! Spin off a new thread.
myThread = New Thread(New ThreadStart(AddressOf GetRawFigures))
myThread.IsBackground = True
myThread.Start()

Procedure GetRawFigures takes an argument as follows:

Private Sub GetRawFigures(ByVal figures As Integer())

End Sub

When I try to provide it as

myThread = New Thread(New ThreadStart(AddressOf GetRawFigures(intArray))

I get an error saying " 'AddressOf' operand must be the name of a method; no
parentheses are needed."

How can I fix this? Thanks.

Jul 21 '05 #1
2 18058
You cannot pass parameters to the procedure using the ThreadStart
delegate.

Instead, you could use the ThreadPool.QueueUserWorkItem, which can take
a single argument as a parameter. Note, the ThreadPool is usually
preferred over creating your own threads explicitly.

If you need to create your own thread explicitly using ThreadStart, you
need to refactor the object that contains your GetRawFigures method.
Change GetRawFigures so that it does not take any parameters, and then
add the old parameter as a property on the object.

Ex (excuse my rusty VB):

Public Class RawFigureCaller
Private myFigures as Integer()

Public New(figures as integer())
myFigures = figures
End Sub

Public Sub GetRawFigures()
'References the class-field myFigures instead of a parameter
End Sub
End Class
Now, you change the calling code to create an instance of this object
Dim caller as RawFigureCaller
'figures contains the integers you want to pass to the method
caller = new RawFiguresCaller(figures)
myThread = New Thread(New ThreadStart(AddressOf caller.GetRawFigures))
Note, you could also call GetRawFigures asynchronously AND pass it a
parameter, by using the BeginInvoke() method on a delegate that matches
your method signature (instead of creating your own thread manually).
Hmm... you might not be able to use delegates in VB.NET. I'm not sure
what the VB.NET equivalent is.

Joshua Flanagan
http://flimflan.com/blog
Job Lot wrote:
How can I pass parameters to a procedure which is called in thread?

'! Spin off a new thread.
myThread = New Thread(New ThreadStart(AddressOf GetRawFigures))
myThread.IsBackground = True
myThread.Start()

Procedure GetRawFigures takes an argument as follows:

Private Sub GetRawFigures(ByVal figures As Integer())

End Sub

When I try to provide it as

myThread = New Thread(New ThreadStart(AddressOf GetRawFigures(intArray))

I get an error saying " 'AddressOf' operand must be the name of a method; no
parentheses are needed."

How can I fix this? Thanks.

Jul 21 '05 #2
Job Lot <Jo****@discussions.microsoft.com> wrote:
How can I pass parameters to a procedure which is called in thread?


See http://www.pobox.com/~skeet/csharp/t...rameters.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Pavils Jurjans | last post: by
2 posts views Thread by Carlos | last post: by
reply views Thread by Paul Allan | last post: by
4 posts views Thread by Mike Dinnis | last post: by
2 posts views Thread by Nab | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.