WPF控制台应用程序不返回提示

WPF控制台应用程序不返回提示

问题描述:

我有一个带有以下应用程序类的wpf控制台应用程序:

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);
    }
}

当我从控制台启动时,文本 test1 出现,但控制台光标只是闪烁,并且直到我按Enter才出现提示。我确实删除了StartupURI语句。
如何强制应用程序像控制台应用程序一样运行并在执行后返回提示? (Windows 7 32位)。

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

不要将其设置为Windows Application输出,而要使用Console Application输出类型。唯一的缺点是,在Windows应用程序运行时,控制台将保持打开状态。我想,既然您可以获得控制台的processId,则启动后也许可以将其杀死。

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.