I am trying to reuse two threads in a while loop like this:
while (condition)
{
thread1.Start();
thread1.Join();
thread2.Start();
}
I am using thread1.Join()
because I want thread2 to wait for the first's thread's finalization.
However, after thread1 runs one time I get the error: 'Thread is running or terminated; it cannot restart.'
From what I read, a thread can only be started once so that would imply that threads in loops are just impossible to work but I would guess that there is a way somehow.
One thing that I forgot to mention is that each thread requires input from the keyboard so it should work like this: thread1 runs -> input from the keyboard -> thread 2 runs -> input from the keyboard.
Last Update: I made it work using this:
int i= 0;
while(condition)
{
thread1[i] = new Thread(() => player1Turn(ref creditP1, ref positionP1, ref creditP2, propertyPrices, playerProperty));
thread1[i].Start();
thread1[i].Join();
thread2[i] = new Thread(() => player2Turn(ref creditP2, ref positionP2, ref creditP1, propertyPrices, playerProperty));
thread2[i].Start();
thread2[i].Join();
i++;
}
I know this is a horrible implementation because I use a bunch of threads but for now it will do. I will surely look into Thread pooling as many of you said. Thanks for your time!