Start multiple threads in a repeat command

1

I have a project where I need to start 30 times a thread that will execute the same method, and wanted to do this in a repeat command, for example:

    for (int i = 0; i < 30; i++)
    { 
       Thread t = new Thread(MetodoVoid); 
       t.Start();
       System.Threading.Thread.Sleep(1000);
    }

But when I use this it says that the thread is already running and the error, how to proceed?

    
asked by anonymous 10.03.2016 / 13:00

2 answers

3

You're playing everything in one variable only. If you want to insist on this form, you have to throw elements from an array or list, so each thread will be in a variable.

But read my review. If you are wrong about something so simple, you should not use something that is so complicated to do right.

var threads = new List<Thread>();
for (int i = 0; i < 30; i++) { 
   threads.Add(new Thread(MetodoVoid)); 
   threads[i].Start();
}
    
10.03.2016 / 13:07
0
List<Thread> lst = new List<Thread>();

for (int i = 0; i < 30; i++)
{
   lst.Add(new Thread(MetodoVoid));
}

lst.ForEach(t => t.Start());
    
10.03.2016 / 13:13