从 C# 中的控制台应用程序退出

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

Exit from a console application in C#

c#consoleexitexit-code

提问by Leandro

I read a lot of things here on stackoverflow, comments, opinions and every kind of ideas. But anyway, I really need an explained way to exit my console application (I'm on VS2010 Framework 4) on C# with a custom error.

我在这里阅读了很多关于 stackoverflow、评论、意见和各种想法的内容。但无论如何,我真的需要一个解释的方法来退出我的控制台应用程序(我在 VS2010 Framework 4 上)在 C# 上出现自定义错误。

The best thing I could read right now is on VB:

我现在能读到的最好的东西是在 VB 上:

Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)

and use ExitProcess(1) for an error or ExitProcess(0)

并使用 ExitProcess(1) 处理错误或 ExitProcess(0)

So my questions are:

所以我的问题是:

  • Is the same than Environment.Exit(1) ?
  • What is the better for an application that runs as automatic job?
  • What means the exit codes, -1, 0, 1, and what are their differences?
  • What about static void ExitProcess(uint uExitCode); ?
  • 与 Environment.Exit(1) 相同吗?
  • 对于作为自动作业运行的应用程序来说,什么更好?
  • 退出代码-1、0、1是什么意思,它们的区别是什么?
  • static void ExitProcess(uint uExitCode); ?

Some previously questions marked as bibliography:

以前标记为参考书目的一些问题:

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

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

http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx

http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx

采纳答案by AnthonyLambert

You can:

你可以:

  1. Call Environment.Exit(int)
  2. Re-declare your Main function as returning int, and return the value from it.
  1. 调用 Environment.Exit(int)
  2. 将 Main 函数重新声明为返回 int,并从中返回值。

回答by PhonicUK

Environment.Exitwill be doing the same thing as ExitProcess(plus perhaps some .Net cleaning up) - the end results are the same.

Environment.Exit将做与ExitProcess(可能加上一些 .Net 清理)相同的事情- 最终结果是相同的。

As for exit codes, they mean whatever you want them to (within reason) - 0 is 'successful', != 0 is 'not successful' but you can use any non-0 value you like to mean whatever you like, it's up to the program using that value to do something useful with it.

至于退出代码,它们的意思是你想要的任何东西(在合理范围内) - 0 是“成功”,!= 0 是“不成功”,但您可以使用任何您喜欢的非 0 值来表示您喜欢的任何内容,就可以了到程序使用该值来做一些有用的事情。

回答by codeteq

1) Environment.Exit is nearly the same,

1)Environment.Exit几乎相同,

2) I guess the windows automation service? try it but i guess i won't work as expected...

2)我猜是windows自动化服务?尝试一下,但我想我不会按预期工作...

3) look at this, Windows Exitcodes

3)看看这个,Windows Exitcodes

4) It's nice but you could use the .Net built in method also

4) 这很好,但你也可以使用 .Net 内置方法

回答by HatSoft

Below are some of the ExitCodes specific to one of your question when using Environment.Exit(ExitCode)

以下是使用时特定于您的问题之一的一些 ExitCodes Environment.Exit(ExitCode)

ERROR_SUCCESS
0 (0x0)
The operation completed successfully.
ERROR_INVALID_FUNCTION
1 (0x1)
Incorrect function.
ERROR_FILE_NOT_FOUND
2 (0x2)
The system cannot find the file specified.

For more ExitCodes with details please see here http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx

有关详细信息的更多退出代码,请参阅此处http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx

回答by perilbrain

-1. Is the same than Environment.Exit(1) ?

-1. 与 Environment.Exit(1) 相同吗?

This is .NET, you barely come to know which WINAPI they have implemented for it without peeping inside with windbg.However nothing has been documented about the similarity in ExitProcessand Environment.Exit.Environment.Exitmay contain more than ExitProcess.

这是 .NET,您几乎不知道他们为它实现了哪个 WINAPI,而无需使用 Windbg 窥探内部。然而,没有任何关于 ExitProcessEnvironment.Exit. Environment.Exit可能包含多个 ExitProcess。

-2. What is the better for an application that runs as automatic job?

-2. 对于作为自动作业运行的应用程序来说,什么更好?

If you a running automated job using some scheduler than I have also other idea of using windows services instead.This will remove any headache of exitprocess.

如果您使用一些调度程序运行自动化作业,而不是我也有其他使用 Windows 服务的想法。这将消除退出过程的任何头痛。

-3. What means the exit codes, -1, 0, 1, and what are their differences?

-3. 退出代码-1、0、1是什么意思,它们的区别是什么?

They are just codes to tell OS the kind of exit. You may have seen programmers specifying return 1on error or return 0on success.This makes you free to use any one of them.

它们只是告诉操作系统退出类型的代码。您可能已经看到程序员指定return 1错误或return 0成功。这使您可以自由地使用它们中的任何一个。

-4. What about static void ExitProcess(uint uExitCode); ?

-4. static void ExitProcess(uint uExitCode); ?

You are free to use unmanaged calls in your application and certainly you'll miss the management activity from Environment.Exit.

您可以在应用程序中自由使用非托管调用,当然您会错过Environment.Exit.

回答by ebayindir

I am answering the question "What means the exit codes, -1, 0, 1, and what are their differences?"

我正在回答“退出代码 -1、0、1 的含义是什么,它们的区别是什么?”的问题。

Different exit codes are just a method of communicating status with caller applications. For example in one of the projects I involved, we used 99 as a warning code. As long as all the applications involved knows what to do when they receive 99 or something else it is fine. In that application if the caller receive anything else then 0 or 99, the caller should abort and in the case of 99 it was free to continue.

不同的退出代码只是与调用者应用程序通信状态的一种方法。例如在我参与的其中一个项目中,我们使用了 99 作为警告代码。只要所有涉及的应用程序知道在收到 99 或其他东西时该做什么就可以了。在该应用程序中,如果调用者收到除 0 或 99 之外的任何其他信息,则调用者应该中止,而在 99 的情况下,它可以自由地继续。