I am trying to learn C# multithreading. This question might be stupid for some but anyways I will still ask it.
Question:
I'm trying to figure out where should I put (if I'm asking the right question)
the thread.wait()
method. So it could show all the child threads in the console. This is the output that I would like to have.
The int num
that I inputted is 3.
This should be the output (I am running this with breakpoint):
This is the current output:
Here is my Code:
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Input number of threads: ");
int num = Convert.ToInt32(Console.ReadLine());
Task[] TaskArray = new Task[num];
for (int i = 0; i <= num-1; i++)
{
TaskArray[i] = Task.Run(() => { DifferentMethod(i); });
}
for (int i = 0; i <= num - 1; i++)
{
TaskArray[i].Wait();
}
}
static void DifferentMethod(object ThreadID)
{
while (true)
{
Console.WriteLine("{0} is Running",ThreadID);
Thread.Sleep(500);
}
}
}
}