Home > Software design >  Why my input commands doesn't execute on the console?
Why my input commands doesn't execute on the console?

Time:11-25

So I recently started learning Threading and I'm not quite familiar with how the things work. I decided to make a simple project of a manual gearbox, where you can shift gears and depending on which gear you are will determine how fast the kilometers will build up. My program runs properly, till the moment when I press the "UpArrow" and nothing happens, the thread stays only on first gear. I know I'm missing something, but cant find it. Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace ManualGearbox
{
    public class Program
    {
        static void Main(string[] args)
        {
            ConsoleKey command = Console.ReadKey().Key;
            int kms = 0;
            var FGthread = new Thread(() => FirstGear(kms));
            var SGthread = new Thread(() => SecondGear(kms));
            var TGthread = new Thread(() => ThirdGear(kms));
            var FourthThread = new Thread(() => FourthGear(kms));
            var FifthThread = new Thread(() => FifthGear(kms));
            
            while (command != ConsoleKey.Spacebar)
            {
                if (command == ConsoleKey.S)
                {
                    FGthread.Start();
                }
                if (command == ConsoleKey.UpArrow)
                {
                    GearUp(FGthread, SGthread, TGthread, FourthThread, FifthThread);
                }
                command = Console.ReadKey().Key;
            }
        }

        static int FirstGear(int kilometers)
        {
            while (true)
            {
                if (kilometers < 20)
                {
                    kilometers  ;
                    Thread.Sleep(500);
                    Console.WriteLine(kilometers   " kilometers");
                }
                else
                {
                    kilometers  ;
                    Thread.Sleep(1500);
                    Console.WriteLine(kilometers   " kilometers");
                }
            }
        }

        static int SecondGear(int kilometers)
        {
            while (true)
            {
                if (kilometers < 40)
                {
                    kilometers  ;
                    Thread.Sleep(500);
                    Console.WriteLine(kilometers   "kilometers");
                }
                else
                {
                    kilometers  ;
                    Thread.Sleep(1500);
                    Console.WriteLine(kilometers   "kilometers");
                }
            }
        }

        static int ThirdGear(int kilometers)
        {
            while (true)
            {
                if (kilometers < 60)
                {
                    kilometers  ;
                    Thread.Sleep(500);
                    Console.WriteLine(kilometers   "kilometers");
                }
                else
                {
                    kilometers  ;
                    Thread.Sleep(1500);
                    Console.WriteLine(kilometers   "kilometers");
                }
            }
        }

        static int FourthGear(int kilometers)
        {
            while (true)
            {
                if (kilometers < 80)
                {
                    kilometers  ;
                    Thread.Sleep(500);
                    Console.WriteLine(kilometers   "kilometers");
                }
                else
                {
                    kilometers  ;
                    Thread.Sleep(1500);
                    Console.WriteLine(kilometers   "kilometers");
                }
            }
        }

        static int FifthGear(int kilometers)
        {
            while (true)
            {
                if (kilometers >= 80)
                {
                    kilometers  ;
                    Thread.Sleep(500);
                    Console.WriteLine(kilometers   "kilometers");
                }
                else
                {
                    kilometers  ;
                    Thread.Sleep(1500);
                    Console.WriteLine(kilometers   "kilometers");
                }
            }
        }

        static void GearUp(Thread first, Thread second, Thread third,
            Thread fourth, Thread fifth)
        {
            if (Thread.CurrentThread == first)
            {
                first.Abort();
                Console.WriteLine("Switching to second");
                second.Start();
            }
            else if (Thread.CurrentThread == second)
            {
                second.Abort();
                Console.WriteLine("Switching to third");
                third.Start();
            }
            else if (Thread.CurrentThread == third)
            {
                third.Abort();
                Console.WriteLine("Switching to fourth");
                fourth.Start();
            }
            else if (Thread.CurrentThread == fourth)
            {
                fourth.Abort();
                Console.WriteLine("Switching to fifth");
                fifth.Start();
            }
        }
    }
}

I would highly appreciate any help.

CodePudding user response:

Thread.CurrentThread is always the current thread. Your GearUp method is never invoked from any place other than from your main(), therefore Thread.CurrentThread will always be the main thread, it will never be any of your other threads.

To fix this you will need to somehow move the checking for the console key and the switching of gears within each one of your gear threads.

Do yourself a favor and quit having a separate method for each gear. They should all be implemented using just one method, which knows which gear it is via a parameter. (Or a separate class, which knows which gear it is via a constructor parameter.)

  • Related