在 C# 中退出控制台应用程序的命令是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10286056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:11:02  来源:igfitidea点击:

What is the command to exit a Console application in C#?

c#console-applicationexit

提问by Sababoni

What is the command in C# for exit a Console Application?

C# 中退出控制台应用程序的命令是什么?

采纳答案by Nikhil Agrawal

You can use Environment.Exit(0);and Application.Exit

您可以使用Environment.Exit(0);Application.Exit

Environment.Exit(0)is cleaner.

Environment.Exit(0)更干净

回答by sinelaw

Several options, by order of most appropriate way:

几个选项,按最合适的方式排序:

  1. Return an int from the Program.Main method
  2. Throw an exception and don't handle it anywhere (use for unexpected error situations)
  3. To force termination elsewhere, System.Environment.Exit(not portable! see below)
  1. 从 Program.Main 方法返回一个 int
  2. 抛出异常并且不在任何地方处理它(用于意外错误情况)
  3. 在其他地方强制终止,System.Environment.Exit不可移植!见下文

Edited 9/2013 to improve readability

编辑 9/2013 以提高可读性

Returning with a specific exit code:As Servy points out in the comments, you can declare Main with an intreturn type and return an error code that way. So there really is no need to use Environment.Exit unlessyou need to terminate with an exit code and can't possibly do it in the Main method. Most probably you can avoid that by throwing an exception, and returning an error code in Main if any unhandled exception propagates there. If the application is multi-threaded you'll probably need even more boilerplate to properly terminate with an exit code so you may be better off just calling Environment.Exit.

返回特定的退出代码:正如 Servy 在评论中指出的那样,您可以使用int返回类型声明 Main 并以这种方式返回错误代码。所以真的没有必要使用 Environment.Exit除非你需要用退出代码终止并且不可能在 Main 方法中执行它。很可能您可以通过抛出异常来避免这种情况,如果任何未处理的异常在 Main 中传播,则在 Main 中返回错误代码。如果应用程序是多线程的,您可能需要更多样板来正确终止退出代码,因此您最好只调用 Environment.Exit。

Another point against using Evironment.Exit- even when writing multi-threaded applications - is reusability. If you ever want to reuse your code in an environment that makes Environment.Exitirrelevant (such as a library that may be used in a web server), the code will not be portable. The best solution still is, in my opinion, to always use exceptions and/or return values that represent that the method reached some error/finish state. That way, you can always use the same code in any .NET environment, and in any type of application. If you are writing specifically an app that needs to return an exit code or to terminate in a way similar to what Environment.Exitdoes, you can then go ahead and wrap the thread at the highest level and handle the errors/exceptions as needed.

反对使用的另一点Evironment.Exit——即使在编写多线程应用程序时——是可重用性。如果您想在Environment.Exit无关紧要的环境(例如可能在 Web 服务器中使用的库)中重用您的代码,则该代码将不可移植。在我看来,最好的解决方案仍然是始终使用异常和/或返回值来表示方法达到某种错误/完成状态。这样,您始终可以在任何 .NET 环境和任何类型的应用程序中使用相同的代码。如果您正在专门编写一个需要返回退出代码或以类似方式终止的应用程序,Environment.Exit那么您可以继续在最高级别包装线程并根据需要处理错误/异常。

回答by Developer

Console applications will exit when the main function has finished running. A "return" will achieve this.

当主函数完成运行时,控制台应用程序将退出。“回归”将实现这一目标。

    static void Main(string[] args)
    {
        while (true)
        {
            Console.WriteLine("I'm running!");
            return; //This will exit the console application's running thread
        }
    }

If you're returning an error code you can do it this way, which is accessible from functions outside of the initial thread:

如果你返回一个错误代码,你可以这样做,它可以从初始线程之外的函数访问:

    System.Environment.Exit(-1);

回答by aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

You can use Environment.Exit(0)and Application.Exit.

您可以使用Environment.Exit(0)Application.Exit

Environment.Exit(): terminates this process and gives the underlying operating system the specified exit code.

Environment.Exit(): 终止此进程并为底层操作系统提供指定的退出代码。