![](/img/trans.png)
[英]C# write a simple string from winforms --> cmd prompt telnet
[英]C# hybird Winforms / console app returns to cmd prompt
我已经看到许多 C# 的混合 gui/cli 应用程序示例。我已经实现了这样一个应用程序,但我很难弄清楚如何防止 .exe 在命令行上运行时不返回到立即提示。
//defines for commandline output
[DllImport("kernel32.dll")]
static extern bool AttachConsole(int dwProcessId);
private const int ATTACH_PARENT_PROCESS = -1;
[STAThreadAttribute]
static void Main(string[] args)
{
// load cli
// redirect console output to parent process;
// must be before any calls to Console.WriteLine()
AttachConsole(ATTACH_PARENT_PROCESS);
if (args.Length == 0)
{
//loads gui
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new form_Main());
}
else
{
cli_main cli = new cli_main();
cli.start_cli(args);
Console.WriteLine("finished");
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
Application.Exit();
}
}
我得到以下 output
C:\Users\john\Documents\Visual Studio 2010\Projects\test\test\test\bin\Debug>test.exe -param1 test
-param2 test2
C:\Users\john\Documents\Visual Studio 2010\Projects\test\test\test\bin\Debug>Output was successful. File saved as: c:\test\test.html
finished
“finished”行是一个字符串 I output 当我知道我已经到达我的主要代码的末尾时...这在 Winforms 中工作正常,我的项目是 Winforms 并且我以 gui 启动它但现在我试图让它混合图形用户界面/命令行
它似乎正在运行我在调试中看到的主要代码和线程,它创建了我的最终 output 文件...
我只是对如何在从 cmd 行执行时保持 .exe 及其参数不返回命令提示符感到困惑? 让它等待闪烁的 cursor,然后是 output 关于 html 文件的行,然后是“完成”行,最后是 go 回到命令提示符。
我试过很多东西,比如删除
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
Application.Exit();
而不是使用Application.Exit();
使用Environment.Exit(0);
但它总是立即返回到命令提示符,我也尝试在该行后睡眠 5 秒
cli.start_cli(args);
但这也不管用,我想我不明白它是如何立即返回命令提示符的,甚至连这条线都没有
Console.WriteLine("finished");
FWIW,我也尝试了第一种方法。 我最终只是隐藏了控制台 window 使用:
IntPtr handle = GetConsoleWindow();
if (handle != IntPtr.Zero)
{
ShowWindow(handle, 0); // 0=SW_HIDE
}
这完全隐藏了 window,甚至在任务栏中也是如此。 它闪烁了一秒钟,但在我的情况下是可以接受的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.