472,353 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 18288
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: zlatko | last post by:
There is a form in an Access Project (.adp, Access front end with SQL Server) for entering data into a table for temporary storing. Then, by...
7
by: Pavils Jurjans | last post by:
Hallo, I have been programming for restricted environments where Internet Explorer is a standard, so I haven't stumbled upon this problem until...
1
by: Maria | last post by:
Hello! I am new to Crystal reports an I have problems passing parameters form outside to Crystal report an creating a report with data from more...
2
by: Carlos | last post by:
Hi all, I am familiar with passing parameters to a thread function using C++, but I needt to learn it using C#. Can someone shed some light on...
0
by: Paul Allan | last post by:
I am new to ASP.net (intermediate ASP developer). I am developing a ASP.net web application and I am having some difficulty calling and passing...
4
by: Mike Dinnis | last post by:
Hi, I've been working through a number of turorials to try to learn more about retrieving data from a SQL database. I think i've mastered...
2
by: Nab | last post by:
I have just tried to pass parameters to a procedure in VB 2005 and realised that you only need to pass the input parameter. The output parameter's...
6
by: Smish | last post by:
hey all, I am passing parameters through code to a stored procedure in both windows application (using c# and sql server & crystal reports)and web...
1
by: goilaswati | last post by:
Hi, I called a function of a class from a form like: myController.BackupProc(myHandle); In the class myController, I have the following...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.