473,414 Members | 1,936 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,414 software developers and data experts.

New Thread with multiple parameters

Is there anyway I can start a get a job done by starting a new thread
with MULTIPLE parameters?
>From what I can see, the BackgroundWorker and ParameterizedThreadStart
operations only take a maximum of 1 parameter?

I need a way of passing (at minimum) at least 3 parameters to the new
thread.
TIA

Dave.

Aug 10 '07 #1
5 34225
Dave,

If you are using .NET 2.0 (and C# 2.0 as a result), you can use
anonymous delegates to pass whatever parameters you want to your thread.
Say you have a method, DoSomething, like so:

void DoSomething(string param1, string param2, string param3)
{
}

Which is what you want to run on the other thread. You can create the
thread and call it like this:

// The parameters.
string param1 = "Hey";
string param2 = "You";
stirng param3 = "There";

// The delegate to call.
ThreadStart ts = delegate() { DoSomething(param1, param2, param3) };

// The thread.
Thread t = new Thread(ts);

// Run the thread.
t.Start();

Now, the only thing you have to be careful of here is that you don't
change the values of param1, param2 and param3 between the time you set
them, and the time you call Start. If you do, then those values are going
to be changed before the delegate is called, and could affect your program
adversely.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Code Monkey" <dl********@gmail.comwrote in message
news:11*********************@x35g2000prf.googlegro ups.com...
Is there anyway I can start a get a job done by starting a new thread
with MULTIPLE parameters?
>>From what I can see, the BackgroundWorker and ParameterizedThreadStart
operations only take a maximum of 1 parameter?

I need a way of passing (at minimum) at least 3 parameters to the new
thread.
TIA

Dave.

Aug 10 '07 #2
Hmmm....

can't say I've investigated anonymous delegates. I'll definately give
this a whirl when I get back in to work Monday.

Many thanks.
Dave.

Aug 10 '07 #3
For the parameter, you could pass a Dictionary, or List, which can contain
more than one parameter.
Within your thread, you can retrieve each parameter you wanted and use them.

BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);

...
Dictionary<string,objectparameters = new
Dictionary<string,object>();
parameters.Add("Count", 1);
parameters.Add("Name", "thread");

worker.RunWorkerAsync(parameters);
}

void worker_DoWork(object sender, DoWorkEventArgs e)
{
Dictionary<string,objectparameters = (Dictionary<string,
object>)e.Argument;

int count = Convert.ToInt32(parameters["Count"]);
string name = Convert.ToString(parameters["Name"]);
...

Ged
"Code Monkey" <dl********@gmail.comwrote in message
news:11*********************@x35g2000prf.googlegro ups.com...
Is there anyway I can start a get a job done by starting a new thread
with MULTIPLE parameters?
>>From what I can see, the BackgroundWorker and ParameterizedThreadStart
operations only take a maximum of 1 parameter?

I need a way of passing (at minimum) at least 3 parameters to the new
thread.
TIA

Dave.
Aug 10 '07 #4
Hi,
"Code Monkey" <dl********@gmail.comwrote in message
news:11*********************@x35g2000prf.googlegro ups.com...
Is there anyway I can start a get a job done by starting a new thread
with MULTIPLE parameters?
>>From what I can see, the BackgroundWorker and ParameterizedThreadStart
operations only take a maximum of 1 parameter?

I need a way of passing (at minimum) at least 3 parameters to the new
thread.
You can just create an array with the values you need and then you pass that
unique array to the thread.
In the same way you can even create a type containing the parameters you
need and then use it as the parameter:

class MyParams
{
public string param1;
public int param2;
.....
}

Thread newThread = new Thread( new ParameterizedThreadStart(DoWork));
MyParams p = new MyParams();
p.param1 = "";
newThread.Start(p);
public static void DoWork(MyParams data)
{

}


Aug 10 '07 #5
I should have mentioned that the parameters are a string, datatable
and integer.

I think I'll go with the anonymous delegate - seems like the best way
to go on this.

Thanks to everyone though - all suggestions greatly received.

Aug 11 '07 #6

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

Similar topics

0
by: milesd | last post by:
Hi, Rather new to MSXML4. I am parsing an XML data-stream over HTTP, and would like to know why I cannot parse XML nodes with multiple parameters. The XML and Code are below, BUT I would like...
6
by: Bore Biko | last post by:
I don't know hopw to do this, like "printf", or "sprintf", I know they use a stack but how to make my own...?? Thanks in advance !! robert.bralic@si.t-com.hr
1
by: Steven K | last post by:
Hello, I am calling a SQL Server 2K parameter query with the following: Dim spWebDocGroup As OleDb.OleDbDataReader Dim prmWebDocGroup As OleDbParameter Dim cmdWebDocGroup As New...
3
by: sck10 | last post by:
Hello, I have a stored procedure that requires 3 parameters. How do you add multiple parameters for a SqlConnection? Thanks, Steven Public strConn = "Data Source=MyServer;Initial...
1
by: LB | last post by:
Hello everybody Using .net 2003 & vb,asp How can i pass multiple parameters in the URL Hyperlink for a datagrid ? or is there somewhere a sample on how to accomplish that ? I know how to pass...
3
by: cdg | last post by:
Is there any reason why code passing multiple parameters with a vector is not legal. I have left out most of the unneccessary details to try and make the problem clearer. Earlier in the program...
0
by: Andrew Brook | last post by:
I have written a WSDL file and im experiencing a problem with specifying parameters for an operation. I was expecting for wsdl.exe to produce an abstract function with three parameters (because of...
1
by: kokababu | last post by:
Hi, I have to send some data to web service. So, I am thinking that which is the best way to send data to web service, xml formatted as single parameter or as multiple parameters depends on...
3
by: raylopez99 | last post by:
I suspect the answer to this question is that it's impossible, but how do I make the below code work, at the point where it breaks (marked below). See error CS0411 This is the complete code. ...
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
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...
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,...
0
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...
0
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...
0
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...

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.