当前位置: 动力学知识库 > 问答 > 编程问答 >

multithreading - c# How to terminate a thread instantly? / How to abort Console.ReadLine?

问题描述:

I'm trying to create a console app with a thread but when I try to close the thread the Console.ReadLine() blocks it. Do you know how to abort a Console.ReadLine() or how to close a thread immediately? Here's my code:

public static void commandHandling()

{

while (Globals.running) // Globals is just a class where I have all of my global vars

{

string command = Console.ReadLine();

switch (command)

{

default:

Console.WriteLine("The command '" + command + "' cannot be found.");

break;

case "exit":

Main.Stop(); // Main is here the "control center" of the app

break;

}

}

}

Here starts the Thread:

Globals.cmdThread = new Thread(new ThreadStart(commandHandling));

Globals.cmdThread.Start();

分享给朋友:
您可能感兴趣的文章:
随机阅读: