使用Environment.ExitCode与从Main返回int [英] Using Environment.ExitCode versus returning int from Main

查看:65
本文介绍了使用Environment.ExitCode与从Main返回int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划在一个shell脚本中使用C#可执行文件的返回码.我有两个选择:

I am planning to use the return code of the C# executable in one of my shell script. I have two options:

从主方法返回整数值

class MainReturnValTest
{
    static int Main()
    {
        //...
        return 0;
    }
}

(OR)

使用环境.使用退出代码退出

class MainReturnValTest
{
    static void Main()
    {
        //...
        Enviroment.Exit(exitCode);
    }
}

使用上述任何一种方法从可执行文件中返回值是否可以?还是其中一个比其他人更受青睐?

Is it fine to use any of the above ways to return value from the executable? Or is one of them preferred over other?

推荐答案

Environment.Exit()是不礼貌的中止.它立即终止该过程.仅当检测到严重故障时才使用它,例如,它适用于AppDomain.UnhandledException事件处理程序.当您的程序由于未处理的异常而即将终止时,它将运行.

Environment.Exit() is a rude abort. It instantly terminates the process. Use it only when you detect a gross failure, it is appropriate in an AppDomain.UnhandledException event handler for example. Which runs when your program is about to terminate because of an unhandled exception.

这是您的主角:异常是信号异常情况的好方法,该异常情况应使用不为0的ExitCode终止程序.实际上,它会自动设置为异常的HResult属性值.不需要代码.

Which is your lead: exceptions are a good way to signal unusual conditions that should terminate the program with an ExitCode that isn't zero. In fact, it automatically gets set to the HResult property value of the exception. No code required.

这篇关于使用Environment.ExitCode与从Main返回int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆