开发者

C# console app - explicit termination (not Env..Exit)

开发者 https://www.devze.com 2023-01-17 12:38 出处:网络
I have heard that on .NET CF Environment.Exit does not work. Is there any 开发者_开发百科universal way how to terminate (in a standard way) the console app? Thank youAn application automatically termi

I have heard that on .NET CF Environment.Exit does not work. Is there any 开发者_开发百科universal way how to terminate (in a standard way) the console app? Thank you


An application automatically terminates when there is no non-background thread running.

A thread automatically stops running when there is no more code to execute.

So, just make your application have no more code to execute when you want it to terminate.

class Program
{
    static void Main()
    {
        ConsoleKeyInfo cki;

        do
        {
            Console.Write("Press a key: ");
            cki = Console.ReadKey(true);
            Console.WriteLine();

            if (cki.Key == ConsoleKey.D1)
            {
                Console.Write("You pressed: 1");
            }
        }
        while (cki.Key != ConsoleKey.D2);

    } // <-- application terminates here
}
0

精彩评论

暂无评论...
验证码 换一张
取 消