New Thread with multiple parameters

  • Thread starter Thread starter Code Monkey
  • Start date Start date
C

Code Monkey

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.
 
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.
 
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.
 
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,object> parameters = new
Dictionary<string,object>();
parameters.Add("Count", 1);
parameters.Add("Name", "thread");

worker.RunWorkerAsync(parameters);
}

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

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



Ged
 
Hi,


Code Monkey said:
Is there anyway I can start a get a job done by starting a new thread
with MULTIPLE parameters?

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)
{

}
 
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.
 
Back
Top