Environment.Exit和简单的回报2的主要区别区别、简单、Environment、Exit

2023-09-04 01:07:32 作者:無奈已被傷

从应用程序之外,是否有

From outside of the application, is there any difference between

...
Environment.Exit(2)

static int Main()
{
    ...
    return 2;
}

推荐答案

最明显的区别是,你可以从你的code的任何地方拨打Environment.Exit。除此之外:

The most obvious difference is that you can call Environment.Exit from anywhere in your code. Aside from that:

主要整理不会终止该进程是否有执行的其他前台线程; Environment.Exit 将取下来的过程呢。 Environment.Exit 终止进程的没有的展开堆栈和(根据我的实验至少)执行finally块。显然,当你从主返回您已经在顶级至于管理code是担心。 在两个给终结过程中的真正的关闭之前执行的机会 Environment.Exit 要求适当的安全权限,这样就不会少受信任的应用程序的工作。 Main finishing won't terminate the process if there are other foreground threads executing; Environment.Exit will take down the process anyway. Environment.Exit terminates the process without unwinding the stack and executing finally blocks (at least according to my experiments). Obviously when you return from Main you're already at the top level as far as managed code is concerned. Both give finalizers a chance to execute before the process really shuts down Environment.Exit demands the appropriate security permission, so won't work for less trusted apps.

在看到问题的更新,我不完全明白你的意思。在这两种情况下,这个过程只会退出以2 code ...

Having seen the question update, I'm not entirely sure what you mean. In both cases the process will just exit with a code of 2...