CLI和GUI应用程序 [英] Both CLI and GUI application

查看:286
本文介绍了CLI和GUI应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写同时具有命令行和GUI应用程序

I am writing an application that has both CLI and GUI.

我读到的大多数问题和文章关于它,并高度发现有用的这个问题:

I read most questions and articles regarding it, and found highly usefull this question:

一个可执行既可以是一个控制台和GUI应用程序?

我的最终代码如下:

        if (args.Length > 0)
        {
            //console code            
        }
        else
        {
            FreeConsole();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form());
        } 

这通过运行双击.exe文件时的伟大工程,或调试时,或者带参数的控制台。

This works great when running the .exe by double click, or when debugging, or from console with arguments.

然而,不带参数的控制台上运行时,该GUI被打开,像我预期的,但在控制台被卡住等待GUI关闭。

However, when running it from the console with no arguments, the GUI is opened, like I intended, but the console is stuck waiting for the GUI to close.

这是不寻常的GUI和控制台的行为。控制台通常启动GUI,不要等它的退出,但对于新的命令。

This is uncharacteristic GUI and console behavior. the console usually launch the GUI and not wait to its exit, but for new commands.

有没有一种方法来避免呢?

Is there is a way to avoid it?

推荐答案

在您链接到的问题接受的答案包含了这样一段话:

The accepted answer in the question you linked to contains this passage:

俊峰的第二个技巧是什么ILDASM使用。他引用使得它在两种模式下运行时的过程
的反汇编的作者经历。
归根结底,这里是什么它:

Junfeng's second technique is what ildasm uses. He quotes the process that ildasm's author went through when making it run in both modes. Ultimately, here's what the it does:

该计划被标记为控制台模式的二进制文件,所以它总是启动
OUT与控制台。这使得输入和输出重定向工作
为正常。如果程序没有控制台模式的命令行参数,
它重新启动本身。这是不够的,简单地调用FreeConsole至
进行一审不再是一个控制台程序。这是因为
启动该程序的进程,CMD.EXE,知道它开始
控制台模式程序,正在等待程序停止运行。
调用FreeConsole将使ILDASM停止使用控制台,但也
不会使父进程开始使用控制台。

The program is marked as a console-mode binary, so it always starts out with a console. This allows input and output redirection to work as normal. If the program has no console-mode command-line parameters, it re-launches itself. It's not enough to simply call FreeConsole to make the first instance cease to be a console program. That's because the process that started the program, cmd.exe, "knows" that it started a console-mode program and is waiting for the program to stop running. Calling FreeConsole would make ildasm stop using the console, but it wouldn't make the parent process start using the console.

要我来说,它看起来就像一个二进制试图控制台子系统和GUI子系统(这确实是不允许的)是更多的努力比它的价值之间进行切换的烦恼。

To me it looks like the headache of having a binary trying to switch between the console subsystem and GUI subsystem (which really isn't allowed) is more effort than it's worth.

一个办法是有一个单独的图形用户界面应用程序.exe。每当控制台应用程序不带参数启动它启动GUI应用程序,并会自行关闭。

One approach would be to have a separate GUI application .exe. Whenever the console app is started without parameters it launches the GUI app and closes itself.

要避免重复代码,这可能需要应用程序的所有实际的逻辑被放在一个单独的类库。

To prevent code duplication this probably requires all the actual logic of the application to be put in a separate class library.

这篇关于CLI和GUI应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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