How to manage the console for program runtime?

I am trying to write a program that works in console or GUI mode, depending on the execution options. I was able to write the following code example:

using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.Runtime.InteropServices; namespace wfSketchbook { static class Program { [DllImport("Kernel32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool AttachConsole(int processId); [DllImport("Kernel32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool AllocConsole(); [DllImport("Kernel32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool FreeConsole(); private const int ATTACH_PARENT_PROCESS = -1; /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { if (args.Length > 0) { if (!AttachConsole(ATTACH_PARENT_PROCESS)) AllocConsole(); Console.WriteLine("Welcome to console!"); Console.ReadKey(); FreeConsole(); } else { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } } } 

It usually works, however, when the program is called from the command line of the system, cmd does not seem to know that this program works in console mode and immediately exits:

 d:\Dokumenty\Dev\C#\Projekty\Win32\Sketchbook\wfSketchbook\bin\Debug>wfSketchbook.exe test d:\Dokumenty\Dev\C#\Projekty\Win32\Sketchbook\wfSketchbook\bin\Debug>Welcome to console! d:\Dokumenty\Dev\C#\Projekty\Win32\Sketchbook\wfSketchbook\bin\Debug> 

I would prefer the following output:

 d:\Dokumenty\Dev\C#\Projekty\Win32\Sketchbook\wfSketchbook\bin\Debug>wfSketchbook.exe test Welcome to console! d:\Dokumenty\Dev\C#\Projekty\Win32\Sketchbook\wfSketchbook\bin\Debug> 

How can I fix this problem?

+3
source share
2 answers

There is no reliable way to make a Windows application a console and a graphical interface. Your program is a Windows application — that's why Windows launches you outside the console window — when your program starts, you are not connected to the console window.

You can change your project to a console application in the project properties. But then you always get a console window. Windows could see that your application was marked as a console application and created the console before it even started.

See the blog post for more information and links to some of the work around.

+1
source

There is no perfect solution for this. Cmd.exe will automatically wait for the program to finish if it sees that .exe is a console mode application. This does not apply to your application. One way to solve this problem is to wait:

start / wait yourapp.exe [arguments]

Another is to always use AllocConsole (). What side effect does the second console window create? Changing the type of application to the Console, and calling FreeConsole () is also not ideal, the flash of the window is quite noticeable. Choose your poison.

+3
source

All Articles