First simple multithreaded application

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)}%");     } 


Category: c# Time: 2016-07-31 Views: 8

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C), All Rights Reserved.

processed in 0.243 (s). 12 q(s)