Both CLI and GUI applications
I am writing an application with CLI and GUI.
I have read most of the questions and articles regarding this and found this question very helpful:
Can a single executable be a console and graphical application?
My final code looks like this:
if (args.Length > 0)
{
//console code
}
else
{
FreeConsole();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form());
}
This works great when running the .exe with a double click or when debugging or from the console with arguments.
However, when run from the console with no arguments, the GUI opens as I expected, but the console gets stuck waiting for the GUI to close.
This is uncharacteristic GUI and console behavior. the console usually launches the GUI and doesn't wait for its exit, but for new commands.
Is there a way to avoid this?
source to share
The accepted answer in the question you mentioned contains this passage:
The second Junfeng technique is what ildasm uses. He cites the process that the author of ildasm went through when it ran in both modes. Ultimately, this is what it does:
The program is marked as a binary version of console mode, so it always runs from the console. This allows you to work with input and output redirection as usual. If the program does not have command line parameters in console mode, it starts again. This is not enough to simply call FreeConsole to make the first instance cease to be a console program. This is because the process that launches the program, cmd.exe, "knows" that it is running the program in console mode and waits for the program to stop running. Calling FreeConsole would cause ildasm to stop using the console, but that would not start the parent process from the console.
It seems to me that the headache associated with binary attempts to switch between the console subsystem and the GUI subsystem (which really isn't allowed) is more effort than it costs.
One approach is to have a separate .exe GUI. Whenever the console application is launched without parameters, it launches the graphical application and exits.
To prevent code duplication, this probably requires that all of the actual application logic be placed in a separate class library.
source to share
This may not be the answer to your direct question, but with this double solution, you are asking for trouble :) This is a hack that will work in some cases but not in others.
The correct solution would be to exclude the functionality and application logic in a separate class library, and then call this "engine" from the console application and GUI. Place all three projects in one Visual Studio solution. All functionality and the vast majority of code should be in that class library, GUI and console projects will only deal with certain aspects that are environment dependent (for example, a button click event will only be in a graphical application, etc.).
source to share
The usual way to do this is to abstract view from logic, and then have two exes, one CLI, one GUI, and not have that. Going down this path leaves you with some kind of terrible trade-off with the benefits of neither approach. A GUI with command line parameters is not a CLI application, it is a GUI with an invisble / short living window.
source to share