从 WPF Windows 应用程序输出 Console.WriteLine 到实际控制台 [英] Output Console.WriteLine from WPF Windows Applications to actual console

查看:79
本文介绍了从 WPF Windows 应用程序输出 Console.WriteLine 到实际控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我正在努力将命令行和批处理功能添加到现有的 WPF Windows 应用程序.当我在启动时检测到一些选项时,我会抑制窗口出现,进行一些处理并立即退出.现在,因为没有 UI,我想将一些消息输出到 stdout/stderr.考虑以下代码:

命名空间 WpfConsoleTest{公共部分类 App : 应用程序{protected override void OnStartup(StartupEventArgs e){Console.WriteLine("开始");System.Threading.Thread.Sleep(1000);Console.WriteLine("停止");关机(0);}}}

当我从命令行运行时,我希望得到以下输出:

开始停止

相反:

C:	est>WpfConsoleTest.exeC:测试>

可以重定向输出,不过:

C:	est>WpfConsoleTest.exe >输出.txtC:	est> 输入 out.txt开始停止

很遗憾,重定向到 CON 不起作用:

C:	est>WpfConsoleTest.exe >康C:测试>

另一个问题是 WpfConsoleTest.exe 在启动后立即退出.所以:

C:	est>WpfConsoleTest.exe >out.txt &输入.txtC:测试>

但是:

C:	est>WpfConsoleTest.exe >out.txt &ping 本地主机 >空和输入.txt开始停止

到目前为止我能想到的最佳解决方案是使用 start/B/wait:

C:	est>start/B/wait WpfConsoleTest.exe >out.txt &输入.txt开始停止

这种方法基本没问题——如果你把它包在一个蝙蝠里,你可以保留错误代码等等.一个巨大的缺点是您在应用程序结束后获得输出,即您无法跟踪进度,您必须等待正在发生的任何事情完成.

因此,我的问题是:如何从 WPF Windows 应用程序 输出到父控制台? 另外,为什么从 WPF 中获取 stdout/stderr 如此困难?>

我知道我可以在项目设置中将应用程序类型更改为控制台应用程序,但这有一个令人讨厌的副作用 - 控制台窗口始终可见,即使您只需双击 exe.这个解决方案也不行,因为它创建了一个新的控制台,即使应用程序是从 cmd 运行.

澄清一下,如果有控制台,我希望我的应用输出到现有控制台,如果缺少则创建一个新控制台.>

解决方案

挖掘了一下,我找到了这个答案.代码现在是:

命名空间 WpfConsoleTest{公共部分类 App : 应用程序{[DllImport("Kernel32.dll")]public static extern bool AttachConsole(int processId);protected override void OnStartup(StartupEventArgs e){附加控制台(-1);Console.WriteLine("开始");System.Threading.Thread.Sleep(1000);Console.WriteLine("停止");关机(0);}}}

直接调用exe还是有副作用的,与调用立即返回有关:

C:	est>WpfConsoleTest.exeC:	est>开始停止^^^^光标会停留在这里等待用户按回车!

解决方案是再次使用start:

C:	est>start/wait WpfConsoleTest.exe开始停止

感谢您的意见!

Background: I am struggling to add command line and batch processing capabilities to an existing WPF Windows Application. When I detect some options at startup I suppress the window from appearing, do some processing and quit immedietally. Now, because there's no UI I'd like to output some messages to stdout/stderr. Consider following code:

namespace WpfConsoleTest
{
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            Console.WriteLine("Start");
            System.Threading.Thread.Sleep(1000);
            Console.WriteLine("Stop");
            Shutdown(0);
        }
    }
}

When I run from command line I would expect following output:

Start
Stop

But instead:

C:	est>WpfConsoleTest.exe

C:	est>

You can redirect output, though:

C:	est>WpfConsoleTest.exe > out.txt

C:	est>type out.txt
Start
Stop

Redirecting to CON does not work, unfortunately:

C:	est>WpfConsoleTest.exe > CON

C:	est>

Another problem is that WpfConsoleTest.exe quits immedietaly after start. So:

C:	est>WpfConsoleTest.exe > out.txt & type out.txt

C:	est>

But:

C:	est>WpfConsoleTest.exe > out.txt & ping localhost > nul & type out.txt
Start
Stop

The best solution I was able to come with so far is to use start /B /wait:

C:	est>start /B /wait WpfConsoleTest.exe > out.txt & type out.txt
Start
Stop

This approach is mostly ok - if you wrap it up in a bat you can preserve error code and so on. The one huge downfall is that you get output after application ends, i.e. there's no way you can track progress, you have to wait for whatever is going on to finish.

Therefore, my question is: How to output to parent console from WPF Windows Application? Also, why is so hard to grab stdout/stderr from WPF?

I know that I could change application type to Console Application in project settings, but this has a nasty side effect - console window is visible all the time, even if you simply double click the exe. This solution also won't do, because it create a new console, even if application was run from cmd.

EDIT: to clarify, I want my app to output to the existing console if there is one and not to create a new one if it is missing.

解决方案

After digging up a bit, I found this answer. The code is now:

namespace WpfConsoleTest
{
    public partial class App : Application
    {
        [DllImport("Kernel32.dll")]
        public static extern bool AttachConsole(int processId);

        protected override void OnStartup(StartupEventArgs e)
        {
            AttachConsole(-1);
            Console.WriteLine("Start");
            System.Threading.Thread.Sleep(1000);
            Console.WriteLine("Stop");
            Shutdown(0);
        }
    }
}

Calling the exe directly still has a nasty side effect, connected with the call returning immediately:

C:	est>WpfConsoleTest.exe

C:	est>Start
Stop

^^^^
The cursor will stay here waiting for the user to press enter!

The solution is, once again, to use start:

C:	est>start /wait WpfConsoleTest.exe
Start
Stop

Thanks for input!

这篇关于从 WPF Windows 应用程序输出 Console.WriteLine 到实际控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆