从cmd运行时控制台和winforms应用程序之间的区别 [英] Difference between console and winforms applications when running from cmd

查看:286
本文介绍了从cmd运行时控制台和winforms应用程序之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个winforms应用程序,有时使用从命令行。
下面是代码(当然简化):

  [STAThread] 
static void Main
{
AttachConsole(ATTACH_PARENT_PROCESS);
Console.WriteLine(Hello);

/*Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); * /
}

如果是控制台应用程序,输出可以是:

  C:\ConsoleApplication\ConsoleApplication.exe 

C:\ConsoleApplication\_

如果是Windows应用程序, / p>

  C:\WindowsApplication\WindowsApplication.exe 
C:\WindowsApplication\Hello
_

任何人都可以告诉我为什么我们有这样的差异,可以使我的Windows应用程序的行为



我想要我的窗口应用程序在从cmd运行时的行为类似于控制台:

  C:\WindowsApplication\WindowsApplication.exe 
Hello
C:\WindowsApplication\_

解决方案: / p>

结果我运行我的应用程序

  \WindowsApplication\start / wait WindowsApplication.exe 


解决方案

是。区别是cmd.exe知道那种可执行文件。它知道等待进程终止,当它是一个控制台模式应用程序。它不会等待它是一个常规的Windows gui应用程序。相信它会创建自己的窗口。所以它再次显示命令提示符,您的输出将追加到。您也将无法使用 Console.ReadLine() btw。



启动/等待yourapp.exe 强制cmd.exe等待。调用 AllocConsole()而不是唯一的通用修复程序。当你的应用程序从一个快捷方式启动时,还要关心创建控制台。



AllocConsole()是相当迷惑的。考虑写一个微小的控制台模式应用程序,除了Process.Start + WaitForExit,只启动你的主程序。也许也调用命令行参数。现在你得到阻塞行为回来。如果你将可执行文件重命名为mainapp.com(启动mainapp.exe),那么它的区别就会隐藏起来,这也是VS使用的一个技巧(devenv.exe vs devenv.com)。


I have a winforms application that sometimes used from the command line. Here is the code (simplified of course):

[STAThread]
static void Main()
{
    AttachConsole(ATTACH_PARENT_PROCESS);
    Console.WriteLine("Hello");

    /*Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());*/
}

If that was a console application the output could be:

C:\ConsoleApplication\ConsoleApplication.exe
Hello
C:\ConsoleApplication\_

In case of windows application its actually:

C:\WindowsApplication\WindowsApplication.exe
C:\WindowsApplication\Hello
_

Can anyone tell me why do we have such difference and is it possible to make my windows application to behave like console when running from cmd?

edit:

I want my windows application to behave like console when running from cmd:

C:\WindowsApplication\WindowsApplication.exe
Hello
C:\WindowsApplication\_

solution:

As a result I'm running my application as

C:\WindowsApplication\start /wait WindowsApplication.exe

解决方案

Yes. The difference is that cmd.exe is aware of the kind of executable. It knows to wait for the process to terminate when it is a console mode app. It does not wait when it is a regular Windows gui app. Trusting that it will create its own window. So it displays the command prompt again, your output gets appended to that. You'll also have trouble using Console.ReadLine() btw.

You'd have to start your program with start /wait yourapp.exe to force cmd.exe to wait. Calling AllocConsole() instead is the only universal fix. Also takes care of creating the console when your app gets started from a shortcut.

AllocConsole() is fairly disorienting. Consider writing a tiny console mode app that does nothing but Process.Start + WaitForExit to start your main program. Perhaps also munging the command line arguments. Now you get the blocking behavior back. If you rename the executable to mainapp.com (to start mainapp.exe) then the difference is hidden quite well, a trick that VS uses as well (devenv.exe vs devenv.com).

这篇关于从cmd运行时控制台和winforms应用程序之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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