HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

First simple multithreaded application

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
simplefirstmultithreadedapplication

Problem

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.W

Solution

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...");
    var sw = Stopwatch.StartNew();
    while (sw.Elapsed.TotalMilliseconds (null);
}

Code Snippets

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...");
    var sw = Stopwatch.StartNew();
    while (sw.Elapsed.TotalMilliseconds < workload * 1000)
    {
        var progress = sw.ElapsedMilliseconds / ((double)workload * 1000);
        Console.WriteLine($"Trhead: #{Thread.CurrentThread.ManagedThreadId} Task:{taskNumber} progress : {progress:p}");
        Thread.Sleep(200);
    }
    Console.WriteLine($"Trhead #{Thread.CurrentThread.ManagedThreadId} done!");
    return Task.FromResult<object>(null);
}

Context

StackExchange Code Review Q#136453, answer score: 5

Revisions (0)

No revisions yet.