-2

I've just started learning C# and I'm trying to figure out threads.

So, I've made a two threads and I would like to stop one of them by pressing x.

So far when I press x it only shows on the console but it doesn't abort the thread.

I'm obviously doing something wronng so can someone please point out what I'm doing wrong? Thank you.

static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            //Creating Threads
            Thread t1 = new Thread(Method1)
            {
                Name = "Thread1"
            };
            Thread t4 = new Thread(Method4)
            {
                Name = "Thread4"
            };

            t1.Start();
            t4.Start();
            Console.WriteLine("Method4 has started. Press x to stop it. You have 5 SECONDS!!!");
            var input = Console.ReadKey();
            string input2 = input.Key.ToString();

            Console.ReadKey();
            if (input2 == "x")
            {
                t4.Abort();
                Console.WriteLine("SUCCESS! You have stoped Thread4! Congrats.");
            };
            Console.Read();

        }

        static void Method1()
        {
            Console.WriteLine("Method1 Started using " + Thread.CurrentThread.Name);
            for (int i = 1; i <= 5; i++)
            {
                Console.WriteLine("Method1: " + i);
                System.Threading.Thread.Sleep(1000);
            }
            Console.WriteLine("Method1 Ended using " + Thread.CurrentThread.Name);
        }

        static void Method4()
        {
            Console.WriteLine("Method4 Started using " + Thread.CurrentThread.Name);
            for (int i = 1; i <= 5; i++)
            {
                Console.WriteLine("Method4: " + i);
                System.Threading.Thread.Sleep(1000);
            }
            Console.WriteLine("Method4 Ended using " + Thread.CurrentThread.Name);
        }
DoomBot
  • 82
  • 8
  • 4
    Don't abort threads, it can leave your program in a unstable state. Learn and try to implement "Cooperative cancelation" instead, Here is a [article from microsoft](https://learn.microsoft.com/en-us/dotnet/standard/threading/cancellation-in-managed-threads) about how to properly cancel managed threads. With the knowledge from the article you can then do "On key down Cancel a thread" instead of aborting the thread. – Scott Chamberlain Jun 21 '20 at 21:21
  • thank you. I'll check it out – DoomBot Jun 21 '20 at 21:29
  • 1
    Aborting threads is a [no-no](https://stackoverflow.com/questions/1559255/whats-wrong-with-using-thread-abort), and also not supported in .NET Core. You get an unconditional `PlatformNotSupportedException` on this platform. – Theodor Zoulias Jun 22 '20 at 00:43

1 Answers1

2

It looks like you have a extra Console.ReadKey(); before if (input2 == "x"), that extra read causes the program to stop and wait before going inside your if statement waiting for a 2nd key to be pressed.

Also input.Key returns a enum, when you do the to string on it the enum will use a capital X because that is what it is set to. Either use input.KeyChar.ToString() to convert it to a string or use

var input = Console.ReadKey();
if (input.Key == ConsoleKey.X)

To compare against the enum instead of a string.

I also recommend you read the article "How to debug small programs", debugging is a skill you will need to learn to be able to write more complex programs. Stepping through the code with a debugger you would have seen input2 was equal to X so your if statement was

if ("X" == "x")

which is not true.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431