Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在键关闭时,中止线程_C#_Multithreading - Fatal编程技术网

C# 在键关闭时,中止线程

C# 在键关闭时,中止线程,c#,multithreading,C#,Multithreading,我刚刚开始学习C#,我正试图找出一些线索 所以,我做了两个线程,我想通过按x来停止其中一个线程 到目前为止,当我按下x时,它只在控制台上显示,但不会中止线程 很明显,我在做错事,所以有人能指出我做错了什么吗?多谢各位 static void Main(string[] args) { Console.WriteLine("Hello World!"); //Creating Threads

我刚刚开始学习C#,我正试图找出一些线索

所以,我做了两个线程,我想通过按x来停止其中一个线程

到目前为止,当我按下x时,它只在控制台上显示,但不会中止线程

很明显,我在做错事,所以有人能指出我做错了什么吗?多谢各位

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);
        }
static void Main(字符串[]args)
{
控制台。WriteLine(“你好,世界!”);
//创建线程
线程t1=新线程(方法1)
{
Name=“Thread1”
};
螺纹t4=新螺纹(方法4)
{
Name=“Thread4”
};
t1.Start();
t4.开始();
WriteLine(“Method4已启动。按x停止。您有5秒!!!”;
var input=Console.ReadKey();
字符串input2=input.Key.ToString();
Console.ReadKey();
如果(输入2==“x”)
{
t4.中止();
控制台.WriteLine(“成功!你停止了线程4!恭喜你”);
};
Console.Read();
}
静态void方法1()
{
WriteLine(“Method1使用“+Thread.CurrentThread.Name”启动);

对于(int i=1;i来说,在
if(input2==“x”)
之前,您似乎有一个额外的
Console.ReadKey();
,该额外读取会导致程序停止并等待进入
if
语句,等待按下第二个键

另外,
input.Key
返回a,当您对其执行to字符串时,枚举将使用大写字母X,因为这是它设置的值。可以使用
input.KeyChar.ToString()
将其转换为字符串,也可以使用

var input = Console.ReadKey();
if (input.Key == ConsoleKey.X)
与枚举而不是字符串进行比较

我还建议您阅读文章“”,调试是您需要学习的技能,以便能够编写更复杂的程序。使用调试器单步执行代码时,您可能会看到
input2
等于
X
,因此您的if语句是

if ("X" == "x")

这是不正确的。

不要中止线程,它会使程序处于不稳定状态。请学习并尝试实现“协作取消”,这里介绍如何正确取消托管线程。根据本文中的知识,您可以执行“按下键取消线程”而不是中止线程。谢谢。我将检查它。中止线程是一种,在.NET Core中也不受支持。在此平台上,您将获得一个无条件的
PlatformNotSupportedException
。这将清除一些问题。感谢您的帮助。