Reputation: 632
So this is something I've always wondered..
Say I want 10 threads called t1 t2 t3 etc... But I don't want to write
Thread t1 = new Thread(run);
Thread t2 = new Thread(run);
Thread t3 = new Thread(run);
...
Rather something like this: (pseudo-code)
for(int i = 0; i <= 10; i++){
Thread t + i = new Thread(run);
}
Is there any way to do this?
Thanks in advance.
Edit: Okay, so this is basically what I wanted to do:
static void Main(string[] args)
{
int n = 0;
Program p = new Program();
List<Thread> threads = new List<Thread>();
for(int i = 0; i <= 10; i++)
{
threads.Add(new Thread(p.run));
}
foreach(Thread t in threads)
{
n++;
t.Name = "Thread " + n;
t.IsBackground = true;
t.Start();
}
Console.ReadKey(false);
}
Thanks all!
Upvotes: 0
Views: 46
Reputation: 44439
No, that's not possible. Why not just this?
for(int i = 0; i <= 10; i++){
new Thread(run);
}
If you really want to keep references to each thread, you can store them in a list.
Upvotes: 2