wpf console application not returning to prompt

回眸只為那壹抹淺笑 提交于 2019-12-23 17:04:02

问题


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).


回答1:


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.



来源:https://stackoverflow.com/questions/7336224/wpf-console-application-not-returning-to-prompt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!