Connecting Tech Pros Worldwide Forums | Help | Site Map

New Thread with multiple parameters

Code Monkey
Guest
 
Posts: n/a
#1: Aug 10 '07
Is there anyway I can start a get a job done by starting a new thread
with MULTIPLE parameters?
Quote:
>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.

Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#2: Aug 10 '07

re: New Thread with multiple parameters


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]
- mvp@spam.guard.caspershouse.com



"Code Monkey" <dlynes2005@gmail.comwrote in message
news:1186762533.371472.14280@x35g2000prf.googlegro ups.com...
Quote:
Is there anyway I can start a get a job done by starting a new thread
with MULTIPLE parameters?
>
Quote:
>>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.
>

Code Monkey
Guest
 
Posts: n/a
#3: Aug 10 '07

re: New Thread with multiple parameters


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.

Guest
 
Posts: n/a
#4: Aug 10 '07

re: New Thread with multiple parameters


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" <dlynes2005@gmail.comwrote in message
news:1186762533.371472.14280@x35g2000prf.googlegro ups.com...
Quote:
Is there anyway I can start a get a job done by starting a new thread
with MULTIPLE parameters?
>
Quote:
>>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.
>
Ignacio Machin \( .NET/ C# MVP \)
Guest
 
Posts: n/a
#5: Aug 10 '07

re: New Thread with multiple parameters


Hi,


"Code Monkey" <dlynes2005@gmail.comwrote in message
news:1186762533.371472.14280@x35g2000prf.googlegro ups.com...
Quote:
Is there anyway I can start a get a job done by starting a new thread
with MULTIPLE parameters?
>
Quote:
>>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)
{

}




Code Monkey
Guest
 
Posts: n/a
#6: Aug 11 '07

re: New Thread with multiple parameters


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.

Closed Thread