C# 什么时候应该使用 Environment.Exit 来终止控制台应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/692323/
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-04 13:42:14  来源:igfitidea点击:

When should one use Environment.Exit to terminate a console application?

c#.netconsole-application

提问by mezoid

I'm maintaining a number of console applications at work and one thing I've been noticing in a number of them is that they call Environment.Exit(0).

我正在维护许多正在工作的控制台应用程序,我注意到其中一些应用程序调用 Environment.Exit(0)。

A sample program would look like this:

示例程序如下所示:

public class Program
{
    public static void Main(string[] args)
    {
        DoStuff();
        Environment.Exit(0);
    }
}

I don't understand what the intent of the original programmer was in doing this? In my mind even without the Environment.Exit statement the program should exit just fine. That said, for one of these programs, it's console window has been remaining even after it was supposed to have closed so I'm really not sure what's going on there....

我不明白原来程序员这样做的意图是什么?在我看来,即使没有 Environment.Exit 语句,程序也应该退出就好了。也就是说,对于这些程序之一,它的控制台窗口即使在它应该关闭之后仍然存在,所以我真的不确定那里发生了什么......

Am I missing something here? Or is there a reason why Environment.Exit should be called in this instance?

我在这里错过了什么吗?或者在这种情况下应该调用 Environment.Exit 的原因是什么?

采纳答案by Jon Skeet

The only reason for calling Exit()as the last line of the Main method is if there might be other foreground threads running. They would stay running if execution just fell off the end of Main. Even in this case, it would usually be a better idea either to put in some explicit graceful termination into the other threads - or make them background threads to start with.

Exit()作为 Main 方法的最后一行调用的唯一原因是可能有其他前台线程正在运行。如果执行刚好从Main. 即使在这种情况下,将一些明确的优雅终止放入其他线程通常是更好的主意 - 或者让它们成为后台线程开始。

If you ever want to return a different exit code from Main, the simpler way to achieve that is to declare it to return int.

如果您想从 返回不同的退出代码Main,实现该目标的更简单方法是将其声明为 return int

In short, I don't think you need Environment.Exit()here, and it's worth asking your colleagues exactly why they're using it - chances are they won't be able to give you a good reason, and it's another bit of fluff you can cut out.

简而言之,我认为您不需要Environment.Exit()这里,值得询问您的同事究竟为什么使用它 - 他们很可能无法给您一个很好的理由,这是您可以剪掉的另一种绒毛出去。

回答by Anand Shah

Basically the statement Environment.Exit(0)tells the Operating system that this is a "clean" exit. There are other numbers as well each with a different meaning like Environment.Exit(1)

基本上该语句Environment.Exit(0)告诉操作系统这是一个“干净”的退出。还有其他数字,每个数字都有不同的含义,例如Environment.Exit(1)

However one thing to note is that the "Main" has been declared as returning nothing "void", so the exit code will really not have a meaning to it.

然而需要注意的一件事是,“Main”已被声明为不返回任何“void”,因此退出代码对其没有任何意义。

Just in case you wanted to know more about the different exit codes have a look here

以防万一您想了解更多有关不同退出代码的信息,请查看此处

system Error Codes

系统错误代码

回答by Robert Paulson

This is [compatibility] for command-line programs to indicate success or failure to an underlying shell, and is inherited from older C-style main loops where the prototype of the main function was

这是命令行程序向底层 shell 指示成功或失败的 [兼容性],并且继承自旧的 C 风格主循环,其中主函数的原型是

int main(void);

int main(int argc, char *argv[]);

The return value of 0 traditionally meant success, while non-zero meant failure or something else, depending on what the programmer decided.

返回值 0 传统上意味着成功,而非零意味着失败或其他,这取决于程序员的决定。

Reference:

参考

wikifor more information on the mainfunction.

有关该main功能的更多信息,请访问wiki

MSDN documenttionon Environment.Exit()

MSDN documenttionEnvironment.Exit()

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

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

回答by Yongil

This is really used when other applications are waiting on the result of your console app. For example, SSRS (tool) can launch a console app, and waits to get back a success of failure response. The Environment.Exit(0) sends back a successful execution message.

当其他应用程序正在等待您的控制台应用程序的结果时,这实际上是有用的。例如,SSRS(工具)可以启动一个控制台应用程序,并等待返回成功的失败响应。Environment.Exit(0) 发回成功执行消息。

回答by Farshid

In SSIS when you have an Execute Process Task and you want to know if process is failure , this method is useful.

在 SSIS 中,当您有一个 Execute Process Task 并且您想知道 process 是否失败时,此方法很有用。

回答by Czipperz

In .net core, as of right now, one must use Environment.Exitto terminate their program on Mac. Purely returning from maindoesn't stop the process and it keeps running until the user aborts it. Patterns like:

在 .net core 中,截至目前,必须使用Environment.Exit终止他们在 Mac 上的程序。纯粹从返回main不会停止进程,它会一直运行直到用户中止它。模式如:

public class Program
{
    public static void Main(string[] args)
    {
        Environment.Exit(TryRun(args));
    }
}

are common in my work place because we want to ship for both Windows and Mac.

在我的工作场所很常见,因为我们希望同时为 Windows 和 Mac 提供服务。

回答by Jeff LaFay

I'm currently using Environment.Exit()in a console app where I don't want to throw ugly exception text back to the console. Instead I just notice the condition, write a user friendly message to the screen about what happened and then call Environment.Exit().

我目前Environment.Exit()在控制台应用程序中使用,我不想将丑陋的异常文本扔回控制台。相反,我只是注意到情况,在屏幕上写一条用户友好的消息,说明发生了什么,然后调用Environment.Exit().

Otherwise, you shouldn't need to call it from Main()in a basic console application.

否则,您不需要Main()在基本控制台应用程序中调用它。