I have a WPF app that, when command line arguments are passed, runs as a command line app. To show / acquire the command line window I use the following code.
When I run the app everything works as expected. If I write anything out to the console during the app then the app doesnt auto exit from the console, I have to press "enter" or basically invoke some kind of ReadLine
- Note I am not using ReadLine
in my app at all. In fact I can make this behaviour happen only using this simple code. How can I prevent the requirement to "press enter" once the app has run?
public static void Main(string[] args)
{
if (args.Length == 0)
{
App app = new App();
app.Run(new MainWindow());
}
else
{
// attach to an parent process console
if (!NativeMethods.AttachConsole(-1))
{
// allocate a new console
NativeMethods.AllocConsole();
}
Console.WriteLine("hey");
NativeMethods.FreeConsole();
}
}
I am using the following methods for Alloc / Attach / Free. Note as per Rohit's suggestion I have tried FreeConsole
directly after the WriteLine
but it still doesnt help
[DllImport("kernel32.dll")]
private static extern bool AllocConsole();
[DllImport("kernel32.dll")]
private static extern bool AttachConsole(int pid);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern int FreeConsole();