C# 控制台应用程序 - 显式终止(不是 Env..Exit)

发布于 2024-09-24 11:50:17 字数 71 浏览 0 评论 0原文

我听说在 .NET CF Environment.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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美煞众生 2024-10-01 11:50:17

当没有非后台线程运行时,应用程序会自动终止。

当没有更多代码可以执行时,线程会自动停止运行。

因此,当您希望应用程序终止时,只需让应用程序不再执行任何代码即可。

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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文