3

I've got a wpf "console" application with the following application class:

public partial class App : Application
{
    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool AttachConsole(uint dwProcessId);

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool FreeConsole();

    const uint ATTACH_PARENT_PROCESS = 0x0ffffffff;

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        AttachConsole(ATTACH_PARENT_PROCESS);

        Console.WriteLine("test1");

        FreeConsole();

        System.Environment.Exit(0);
    }
}

When i start it from the console the text "test1" appears but then the console cursor just blinks and the prompt does not appear until I hit Enter. I did remove the StartupURI statement. How to force the application to behave like a console app and return to prompt after the execution? (Windows 7 32 bit).

Greg
  • 1,227
  • 5
  • 23
  • 52
  • 2
    Adding the WPF libraries to a program does not make a program a WPF application any more than adding the DirectX libraries make your program a DirectX application. If you want a console application, start with a console template. Adding Windows templates will only conflict with your goals, and waste a lot of people's time trying to help you. – Bahri Gungor Sep 07 '11 at 15:18
  • the application is supposed to work in both GUI and GUI-less/command line modes. – Greg Sep 07 '11 at 15:53
  • 1
    In that case I would still start with the command line application template, then construct the WPF Application/Bootloader objects only if you need them. Your application startup code should reside in a static Main method. As you have it, you are relying on the default WPF startup code, but then terminating the application on the OnStartup event-handler. – Bahri Gungor Sep 07 '11 at 16:02
  • I have the exact same issue, did you manage to fix it? – Chris Apr 10 '14 at 13:01

1 Answers1

0

Instead of setting it as a Windows Application output, use a Console Application output type. The only drawback is that the console will stay open while the windows application is running. I suppose since you can get the processId of the console, you might be able to kill it after launch.

Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
Bahri Gungor
  • 2,289
  • 15
  • 16
  • have you actually tested it? it doesn't fix the problem I mentioned. if I use the console application as output type the console pops up when I run the application - I do not want that... – Greg Sep 07 '11 at 22:20
  • The output type of the application isn't as relevant as the program structure. Currently, you are using the App.OnStartup() event method to run your application. By using the App class, you are ceding control of the startup to WPF/Windows. The entry point of the program is hidden from you. However, if you change your application to startup from a Main method, you can bypass the WPF startup for when you want the program to run in console mode. I will write a small startup class for you as an example in a couple minutes. – Bahri Gungor Sep 08 '11 at 13:59
  • exactly what I did - but when I start it from prompt it won't go back to the prompt as it would for a "console application". it will show the text "test1" and blink the cursor until I hit enter. – Greg Sep 08 '11 at 15:06