c# - 启动 C# Windows 应用程序并等待在命令行中退出

标签 c# windows console-application windows-applications

通常,在命令提示符下调用 Windows 应用程序不会等待其退出。一旦我输入 myFormApp.exe 并按回车键,将显示一个表单,命令提示符立即移至下一行。

C:\> myFormApp.exe
C:\>     # this line will display immediately

我知道使用 cmd/c 将等待应用程序退出。

C:\> cmd /c myFormApp.exe
C:\>     # this line will display after myFormApp.exe closed

但即使没有cmd/c我也想等待。

有没有办法通过编辑 myFormApp.exe 源代码来防止命令提示符移动到新行?

AllocConsole 添加到 windows 应用程序将显示控制台,但不会使命令提示符等待退出。

if (args.Length > 0)
{
    // Command line given, display console
    AllocConsole();
}
else
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

任何建议将不胜感激。谢谢!

最佳答案

无需编写额外的应用程序。这样的一个已经存在于 Windows 中 - start with /wait 选项。它不仅会等待您的应用程序完成执行,还会设置返回的错误级别(也称为 ExitCode)。

关于c# - 启动 C# Windows 应用程序并等待在命令行中退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22375282/

相关文章:

windows - Windows Vista 上的 Sublime Text 2 文件关联

windows - 从一组 jpeg 图像创建动画 gif

C#控制台应用程序无法调试,很奇怪

c# - 如何从内循环跳回外循环?

c# - 公共(public)静态字符串 MyFunc() 上的 "Expected class, delegate, enum, interface or struct"错误。什么 's an alternative to "字符串”?

c# - 如何为所有 IEnumerable<Int32> 覆盖 ToString() 方法?

windows - xampp 控制面板中的 apache 和 mysql 启动按钮被禁用

graphics - 谁能推荐一种语言来模仿/重拍原始的俄勒冈小道?

c# - 我应该先在文本模式下开发我的游戏创意吗?

c# - 具有受控并行性和 AwaitAll 选项的异步任务