【问题标题】:Terminating a c# Console application终止 c# 控制台应用程序
【发布时间】:2012-11-13 08:41:14
【问题描述】:

我正在尝试关闭我的 c# 控制台应用程序(在没有调试的情况下运行)并且没有任何工作...我已经尝试了所有具有不同退出代码的常见嫌疑人,但没有任何内容终止它 =/ 是因为该选项位于一堆嵌套的 if 语句?它可能是我想念的非常简单的东西,但它现在伤害了我的大脑,请有人帮忙!我试过了:

System.Environment.Exit(0);
System.Environment.Exit(1);
System.Environment.Exit(-1);
return;
Application.Exit(); //(wont even except it)

如果上下文有帮助,我使用嵌套 if 语句来检查用户是否输入了数字或字母“q”,如果他们输入了数字,则执行计算,如果输入了字母 q,则程序是退出并输出任何其他错误语句。

string userInput;
int userInputDigit = 0;
double userCost = 0;
char userInputChar;

userInput = Convert.ToString(Console.ReadLine());

if (int.TryParse(userInput, out userInputDigit))
{
    if (userInputDigit <= 50)
    {
        userCost = (price * userInputDigit);
        Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
    }
    else if ((userInputDigit > 50) && (userInputDigit <= 80))
    {
        userCost = (price * 50) + ((userInputDigit - 50) * (price - 1));
        Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
    }
    else if ((userInputDigit > 80) && (userInputDigit <= 100))
    {
        userCost = (price * 50) + (30 * (price - 1)) + ((userInputDigit - 80) * (price - 2.50));
        Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
    }
    else
    {
        Console.WriteLine("Error! Please input a number between 0 and 100");
    }

}
else if (char.TryParse(userInput, out userInputChar))
{
    if ((userInput == "q") || (userInput == "Q"))
    {
        System.Environment.Exit(0);
    }
    else
    {
        Console.WriteLine("Incorrect Letter Inputted");
    }
}
else
{
    Console.WriteLine("Error! Please input a number or 'q' to quit");
}

【问题讨论】:

  • 你是如何运行它的?您是否在 Visual Studio 中选择“调试--> 不调试启动”?您是否手动打开控制台窗口,然后输入 .exe 名称?
  • 所以您已经编写了一个控制台程序,并且您可能从命令行运行它。当执行到达主函数的末尾时,它应该自行退出。如果您注释掉 main 中的所有内容,构建程序并运行它,它应该会运行并退出。是吗?
  • 在调试器中检查System.Environment.Exit(0)是否被执行。
  • 如果可能的话,最好看看代码本身。否则,您是否退出 Main 方法(通过 return;从 Main 方法中)?
  • 是的,我正在使用没有调试的启动我想绕过按任意键退出我希望按下字母“q”(按回车键提交)退出它...我'不知道如何检查它是否被执行了

标签: c# console exit terminate


【解决方案1】:

我认为您需要做的是右键单击您的项目,选择“属性”,然后(引用 msdn.com):

在 Visual Studio 开发环境中设置此链接器选项

打开项目的“属性页”对话框。有关详细信息,请参阅设置 Visual C++ 项目属性。

单击链接器文件夹。

单击系统属性页。

修改子系统属性。

对于子系统,选择控制台。希望这可以帮助! :)

【讨论】:

  • 这是否适用于 c# 应用程序?
【解决方案2】:

我建议你试试 application.close() 函数。它多次拯救了我的一天。如果没有帮助,请告诉我。

【讨论】:

  • 我试过了=[由于某种原因它不喜欢“应用程序”(它没有出现在下拉菜单中)
【解决方案3】:

我会首先在调试中运行您的应用程序并检查以确保您的 Application.Exit 确实被命中。这可能有助于了解应用程序未退出的原因。

您应该使用 Environment.Exit(0)。这似乎是结束控制台应用程序的更好方法。

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

如果此代码在 Main 中,我建议使用 return;在您调用退出应用程序之后。

【讨论】:

  • 是的,它在调试时结束,但我打算在环境中运行它并使用代码根据请求退出我已经尝试过 environment.exit(0);但它仍然说“按任意键继续”
  • 在您调用退出应用程序之前是否有 Console.ReadLine()?
  • 或者,您是否在调试时使用 #if debug# 之类的构建时间指令来执行某些操作?
  • 不,readline 在所有 if 语句之前...上面代码的结尾是我的程序的绝对结尾(还有一些花括号)
  • 有趣的是,我在一个新的控制台应用程序(在 Visual Studio 2010 中)中运行了您的代码,它运行并退出了该应用程序就好了。我认为你的逻辑可能是合理的。您可能需要查看上面 Alicja Puacz 的回复。您可能有一些古怪的项目属性需要更改才能在发布模式下工作。
【解决方案4】:

使用循环,让 Main 正常返回。此外,我还尝试简化条件检查以及字符串比较和解析。您的错误消息表明前面的 if/else if 逻辑实际上并未强制执行验证范围(“介于”0 和 100 之间)。例如,如果用户输入负值,您的第一种情况 (...

static bool ExitRequired(string line)
{
    return string.Equals(line, "q", StringComparison.OrdinalIgnoreCase);
}

static void Main(string[] args)
{
    const double price = 10;
    int userInputDigit;
    double userCost;
    string line = null;

    while (!ExitRequired(line))
    {
        Console.WriteLine("Enter a number or press 'q' to exit...");
        line = Console.ReadLine();

        if (ExitRequired(line))
            break;

        if (int.TryParse(line, out userInputDigit) 
            && userInputDigit > 0 
            && userInputDigit < 100)
        {
            if (userInputDigit <= 50)
            {
                userCost = (price * userInputDigit);
                Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
            }
            else if ((userInputDigit > 50) && (userInputDigit <= 80))
            {
                userCost = (price * 50) + ((userInputDigit - 50) * (price - 1));
                Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
            }
            else if ((userInputDigit > 80) && (userInputDigit <= 100))
            {
                userCost = (price * 50) + (30 * (price - 1)) + ((userInputDigit - 80) * (price - 2.50));
                Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
            }
        }
        else
        {
            Console.WriteLine("Error! Please input a number between 0 and 100");
        }
    }
}

【讨论】:

  • 谢谢!我会试一试...价格是在程序的早些时候声明的,因为我在循环中使用它以 10 为增量输出小部件的价格...我只使用了一个 for 循环,并且演示循环是分配目的,所以它可能会受益我的理解!这个(do while?)循环会在用户输入 q 之前让程序要求输入吗?
  • 正确...如果这有帮助,请将其标记为答案,或者让我知道我缺少什么,我会尽力改进,谢谢。
  • 我会做,但要到明天下班后才有机会,所以到时告诉你=]
  • 我已经尝试过了,但如果用户输入 q,它实际上并没有退出程序...我可以将它添加为另一个 if 语句吗?
  • 没关系,我将最后的 else 语句移到嵌套 ifs 的末尾并将 close 程序放在它的位置 =] 非常感谢那个循环!
【解决方案5】:

如果您从 Visual Studio 启动控制台应用程序,而不是处于调试模式 (Ctrl+F5),则控制台窗口在应用程序完成执行后保持打开状态。并提示Press any key to continue...同样,此时您的控制台应用程序已退出。

当您运行 *.exe 文件时,您的控制台将在不要求按键的情况下关闭。这只是 Visual Studio 的“功能”——它运行 cmd.exe 告诉它运行您的可执行文件然后暂停。

这是在您启动应用程序而不进行调试时 Visual Studio 所做的(您可以将其复制到 *.bat 文件并运行它)

ECHO OFF
CLS
"Path\To\Your\ConsoleApplication.exe"
PAUSE

【讨论】:

  • 是的,他们是我的想法,但作业要求“q”是终止程序,我们没有被告知调试我们的程序 =/ 感谢大家的帮助,我将不得不给我的导师发电子邮件并要求她解释任务要求的内容=/
  • 啊,所以我猜是任务是什么沟通不畅......谢谢=]
【解决方案6】:

当您从 Visual Studio 运行项目时,会添加“按任意键继续”文本。

相反,转到垃圾箱并从那里运行它。

确保在运行项目之前构建项目。如果你从 VS 运行,它会在需要时自动构建,但从 Windows 资源管理器运行文件不会这样做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 2013-12-01
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    相关资源
    最近更新 更多