简体   繁体   中英

On key down, abort a thread

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM