473,387 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 18376
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 clicking a botton, several action stored procedures...
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 now, when I need to write a DOM-compatible code. ...
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 than one table This is the problem: I have to...
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 how to do this? Code snippets will be great to show...
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 parameters to a function that is declared in my...
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 techniques where i create a sql string in the page and...
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 value will be returned without the need to pass it...
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 application(using asp.net & sql server & crystal...
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 code: public virtual void BackupProc(IntPtr...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.