开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜