473,779 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Add ressOf GetRawFigures))
myThread.IsBack ground = True
myThread.Start( )

Procedure GetRawFigures takes an argument as follows:

Private Sub GetRawFigures(B yVal figures As Integer())

End Sub

When I try to provide it as

myThread = New Thread(New ThreadStart(Add ressOf GetRawFigures(i ntArray))

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 18406
You cannot pass parameters to the procedure using the ThreadStart
delegate.

Instead, you could use the ThreadPool.Queu eUserWorkItem, 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 RawFiguresCalle r(figures)
myThread = New Thread(New ThreadStart(Add ressOf caller.GetRawFi gures))
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(Add ressOf GetRawFigures))
myThread.IsBack ground = True
myThread.Start( )

Procedure GetRawFigures takes an argument as follows:

Private Sub GetRawFigures(B yVal figures As Integer())

End Sub

When I try to provide it as

myThread = New Thread(New ThreadStart(Add ressOf GetRawFigures(i ntArray))

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****@discuss ions.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.co m>
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
17357
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 (update, append) should be activated in order to transfer data to other tables. I tried to avoid any coding in VB, as I am not a professional, but I have found a statement in an article, that, unlike select queries, form's Input Property can't be...
7
49582
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. The question is about best practices for passing parameters to an event function. I have, say, the following HTML:
1
3636
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 make a report( VS.NET, C#, Web Form) with 3 parts and with data from three tables: 1st part: all the field values form table1 coresponding to an Id (Id
2
2189
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 me. Thanks in advance, Carlos
0
1553
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 "codebehind" page. Function declaration: Sub MyFunction(ByVal sender As Object, ByVal e As System.EventArgs)
4
3001
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 pass it to the Db and retrieveing data from a stored procedure, but I can't get the hang of parameters. I have a method where I can get the parameters passed to the sp but it doesn't want to return any results. Here's a copy of my code:
2
2027
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 as was the case in the previous version of VB e.g. VB .Net 2003. Here's the procedure in the web service: Public Sub Convert2Dollar(ByVal euroAmount As Double, ByRef usDollarAmount As Double) Dim exchangeRate As Double exchangeRate = 10 / 6
6
7821
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 reports)... the windows application works perfectly.. bt web apllication gives exception..."Invalid Table Number"... Can nebdy help...Please......
1
1082
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 thisHandle)
0
9632
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9471
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8958
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3631
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2867
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.