通常,在命令提示符下调用 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
和 /wait
选项。它不仅会等待您的应用程序完成执行,还会设置返回的错误级别(又名 ExitCode)。
关于c# - 启动一个 C# windows 应用程序并等待在命令行中退出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22375282/