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.