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 int timeToRunSecondThread;
private static Thread firstThread = new Thread(FirstThreadFunction);
private static Thread secondThread = new Thread(SecondThreadFunction);
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();
secondThread.Start();
Thread firstThreadChecker = new Thread(FirstThreadCheckerFunction);
Thread secondThreadChecker = new Thread(SecondThreadCheckerFunction);
firstThreadChecker.Start();
secondThreadChecker.Start();
Console.ReadKey();
}
private static void FirstThreadCheckerFunction()
{
while (firstThread.IsAlive)
{
WriteThreadProgressToScreen("Thread #1", timeToRunFirstThread, sw.ElapsedMilliseconds);
Thread.Sleep(100);
}
Console.WriteLine("Thread #1 Finished !");
}
private static void SecondThreadCheckerFunction()
{
while (secondThread.IsAlive)
{
WriteThreadProgressToScreen("Thread #2", timeToRunSecondThread, sw.ElapsedMilliseconds);
Thread.Sleep(100);
}
Console.WriteLine("Thread #2 Finished !");
}
private static void FirstThreadFunction()
{
Thread.Sleep(timeToRunFirstThread);
}
private static void SecondThreadFunction()
{
Thread.Sleep(timeToRunSecondThread);
}
private static void WriteThreadProgressToScreen(string threadName, int timeToRunThread, long currentElapsedMiliseconds)
{
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)}%");
Thread.Sleep(100);
}
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;
}
}