473,503 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

threading parameter

Hello,

This might sound as a stupid question, but how can I pass parameters to a
thread? Let me rephrase that.

When I have a method, i declare it like this:

public void DoSomething(string aParameter, int anotherParameter) {
....
}

When I want to use this method with a thread I do it like this:
Thread doSomethingThread= new Thread(new ThreadStart(this.DoSomething));

doSomethingThread.Start();

Where do I add the parameters?

Nov 15 '05 #1
2 3547
grmAbay <gr*****@hotmail.com> wrote:
This might sound as a stupid question, but how can I pass parameters to a
thread?


Not a stupid question at all - see
http://www.pobox.com/~skeet/csharp/threadstart.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2
100
Hi,
In addition to what you can find in Jon's site I want to give you one more
idea.
You can decalre a delegate for the method you want to start in a thread.

//Delegate and the method itslef might have a return value as well
delegate void ThreadMethod(string aParameter, int anotherParameter) ;

public void DoSomething(string aParameter, int anotherParameter) {
....
}

ThreadMethod starter = new ThreadMethod(DoSomething);

Now you can start DoSomething in a separate thread:

starter.BeginInvoke(param1, param2, null, null);

You can use IAsyncResult returned by BeginInvoke and use EndInvoke to wait
for the thread to finish and get result. You can optain a wait handle,
checking periodically IAsyncResult.IsCompleted or using callback method. You
have options.

Begin invoke uses a thread form the thread pool

More info you can find in MSDN
ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpovrasynchronousprogrammingo
verview.htm

HTH
B\rgds
100

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
grmAbay <gr*****@hotmail.com> wrote:
This might sound as a stupid question, but how can I pass parameters to a thread?


Not a stupid question at all - see
http://www.pobox.com/~skeet/csharp/threadstart.html

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

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6676
by: Savagesmc | last post by:
Problem Background: I am working on a thin wrapper for the pthreads interface on linux, and have encountered frustrating behavior. The idea is to have a "PosixThread" class that any class can...
15
2427
by: DanielEKFA | last post by:
Hey there :) Just joined the group, looking for a resolution to a problem I and my three groups members are having (we're students, making an FTP-like server/client as an exam project). We're...
3
5955
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional...
6
3251
by: MPH Computers | last post by:
Hi I am looking for some help on Threading and Critical Sections I have a main thread that controls an event the event handler creates a new thread for carrying out the work because the...
1
969
by: Pug Fugly | last post by:
I have a function that takes in a parameter, and in the function it calls the .BeginInvoke on the worker delegate. I know that the System.AsyncCallback callback function takes in IAsyncResult as...
0
1970
by: Colmeister | last post by:
I recently read Jason Clark's excellent article on Unhandled Exceptions (http://msdn.microsoft.com/msdnmag/issues/04/06/NET/default.aspx) and have attempted to incorporate the features he talks...
7
339
by: Phill W. | last post by:
Can anyone recommend a good reference on Threading (Framework 1.1)? I'm particularly confused about how to call a method in my "main" thread from another one. - or more, one day ;-) I've...
7
1444
by: ChrisM | last post by:
Hi, I'm trying to get my head round Async. procedure calls. I have the following code: delegate double GetLabourWIPDelegate(DateTime reqDate); .... GetLabourWIPDelegate...
5
10870
by: Miro | last post by:
I will try my best to ask this question correctly. I think in the end the code will make more sence of what I am trying to accomplish. I am just not sure of what to search for on the net. I...
2
1677
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I've two questions regarding the System.Threading.Thread namespace. First, what is the point of having a ThreadStart parameter? For example... using System.Threading;...
0
7205
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
7287
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,...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7006
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...
1
5021
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...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1519
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
397
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...

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.