简体   繁体   English

按下键,中止线程

[英]On key down, abort a thread

I've just started learning C# and I'm trying to figure out threads.我刚刚开始学习 C# 并且正在尝试找出线程。

So, I've made a two threads and I would like to stop one of them by pressing x.所以,我创建了两个线程,我想通过按 x 来停止其中一个线程。

So far when I press x it only shows on the console but it doesn't abort the thread.到目前为止,当我按 x 时,它只显示在控制台上,但不会中止线程。

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();看起来你有一个额外的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.if (input2 == "x")之前,额外的读取会导致程序在进入if语句等待按下第二个键之前停止并等待。

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.input.Key返回一个enum ,当您在其上执行 to 字符串时,枚举将使用大写 X,因为这是它的设置。 Either use input.KeyChar.ToString() to convert it to a string or use使用input.KeyChar.ToString()将其转换为字符串或使用

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使用调试器单步执行代码,您会看到input2等于X ,因此您的 if 语句是

if ("X" == "x")

which is not true.这不是真的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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