using System;
using System.Collections.Generic;
using System.Threading;
namespace pc.threadingExample
{
class Program
{
static void
LongRunningMethod(object name)
{
Thread.Sleep(5000);
Console.WriteLine($"Hello {(string)name}");
}
static void Main(string[] args)
{
char inputChar;
var threads = new List<Thread>();
do
{
Console.WriteLine("1)say hi \n0)exit\n");
inputChar = Console.ReadKey().KeyChar;
if (inputChar == '1')
{
var t = new Thread(LongRunningMethod);
t.IsBackground = true;
t.Start("pawel");
threads.Add(t);
}
} while (inputChar != '0');
foreach (var t in threads)
{
Console.WriteLine($"thread:{t.ManagedThreadId} joining");
t.Join();
}
}
}
}
now full disclosure time odds are you'll never be creating threads but instead you'll use either the async/await keywords or the TPL task parallel library.