It's been quite some time since I got my eye on the multi-threading, today I decided to create a really simple application which runs 2 while loops simultaneously and prints the current progress of the thread to the console window, each thread's run time is taken as an input from the user i.e one thread can run for 5 secs the other one can run for 10. I'm quite unhappy with how it looks but since I cant seem to use methods with parameters, I'm kinda stuck. Any improvements considering the code style, performance and overall design are welcome.

    private static int timeToRunFirstThread;
private static Stopwatch sw;
private static void Main(string[] args)
{
sw = Stopwatch.StartNew();
}

{
{
}
}

{
{
}
}

{
}

{
}

{
Console.WriteLine($"{threadName} progress : {Math.Round(currentElapsedMiliseconds / (double)timeToRunThread * 100, 1)}%"); }  And that's my second approach where I tried to reduce the threads as much as possible and also the repetitive code : public class Program { private static int timeToRunFirstThread; private static int timeToRunSecondThread; private static Thread firstThread = new Thread(ThreadChecker); private static Thread secondThread = new Thread(ThreadChecker); private static Stopwatch sw; private static void Main(string[] args) { timeToRunFirstThread = int.Parse(Console.ReadLine()); timeToRunSecondThread = int.Parse(Console.ReadLine()); timeToRunFirstThread *= 1000; timeToRunSecondThread *= 1000; sw = Stopwatch.StartNew(); firstThread.Start(new ThreadParameters("Thread #1", timeToRunFirstThread, firstThread)); secondThread.Start(new ThreadParameters("Thread #2", timeToRunSecondThread, secondThread)); Console.ReadKey(); } private static void ThreadChecker(object input) { ThreadParameters threadParameters = (ThreadParameters)input; while (sw.ElapsedMilliseconds < threadParameters.TimeToRun) { Console.WriteLine($"{threadParameters.Name} progress : {Math.Round(sw.ElapsedMilliseconds / (double)threadParameters.TimeToRun * 100, 1)}%");
}
Console.WriteLine($"{threadParameters.Name} Finished !"); } } public class ThreadParameters { public string Name { get; private set; } public int TimeToRun { get; private set; } public Thread CurrentThread { get; private set; } public ThreadParameters(string name, int timeToRun, Thread currentThread) { Name = name; TimeToRun = timeToRun; CurrentThread = currentThread; } }  ## 2 Answers It feels a bit like you might be looking for how to start a parameterised thread. In the code below, I've adapted your code slightly so that ThreadChecker is aware of what type to expect. This is initialized when the thread is created, which means that the parameters can't pass the thread object as part of ThreadParameters, however this isn't being used currently anyway and could be retrieved through Thread.CurrentThread if required. public class Program { private static int timeToRunFirstThread; private static int timeToRunSecondThread; private static Stopwatch sw; private static void Main(string[] args) { timeToRunFirstThread = int.Parse(Console.ReadLine()); timeToRunSecondThread = int.Parse(Console.ReadLine()); timeToRunFirstThread *= 1000; timeToRunSecondThread *= 1000; sw = Stopwatch.StartNew(); Thread firstThread = new Thread(() => ThreadChecker(new ThreadParameters("Thread #1", timeToRunFirstThread))); Thread secondThread = new Thread(() => ThreadChecker(new ThreadParameters("Thread #2", timeToRunFirstThread))); firstThread.Start(); secondThread.Start(); Console.ReadKey(); } private static void ThreadChecker(ThreadParameters threadParameters) { while (sw.ElapsedMilliseconds < threadParameters.TimeToRun) { Console.WriteLine($"{threadParameters.Name} progress : {Math.Round(sw.ElapsedMilliseconds / (double)threadParameters.TimeToRun * 100, 1)}%");
Console.WriteLine($"{threadParameters.Name} Finished !"); } } public class ThreadParameters { public string Name { get; private set; } public int TimeToRun { get; private set; } public ThreadParameters(string name, int timeToRun) { Name = name; TimeToRun = timeToRun; } }  Wouldn't it be easier to use new async/await? • You can pass parameters without helper objects • You can wait until all tasks are finished • You can easy create as many tasks as you want • You can display % as {progress:p} which makes the % sign and the Math.Round unnecessary For example: private static void Main(string[] args) { RunFoos(3, 5); } static void RunFoos(params int[] workloads) { var tasks = workloads.Select((w, i) => Task.Factory.StartNew(() => Foo(w, i))); Task.WaitAll(tasks.ToArray()); } static Task Foo(int workload, int taskNumber) { Console.WriteLine($"Trhead: #{Thread.CurrentThread.ManagedThreadId, 2} Task: {taskNumber} working for {workload} sec...");
Console.WriteLine($"Trhead: #{Thread.CurrentThread.ManagedThreadId} Task:{taskNumber} progress : {progress:p}"); Thread.Sleep(200); } Console.WriteLine($"Trhead #{Thread.CurrentThread.ManagedThreadId} done!");