469,312 Members | 2,503 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,312 developers. It's quick & easy.

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 33483
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Steven K | last post: by
3 posts views Thread by sck10 | last post: by
3 posts views Thread by cdg | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.