Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output Console.WriteLine from WPF Windows Applications to actual console

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:\test>WpfConsoleTest.exe  C:\test> 

You can redirect output, though:

C:\test>WpfConsoleTest.exe > out.txt  C:\test>type out.txt Start Stop 

Redirecting to CON does not work, unfortunately:

C:\test>WpfConsoleTest.exe > CON  C:\test> 

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

C:\test>WpfConsoleTest.exe > out.txt & type out.txt  C:\test> 

But:

C:\test>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:\test>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.

like image 567
gwiazdorrr Avatar asked May 02 '12 14:05

gwiazdorrr


People also ask

How do I check output of console WriteLine?

In Visual Studio uppermost menu choose Debug > Windows > Output. It shows all Console. WriteLine("Debug MyVariable: " + MyVariable) when you get to them.

How do you get the console WriteLine output code in Visual Studio?

It will show your output if you will press ctrl+F5. You will get the output in console window.

Where does console WriteLine write to?

Console. WriteLine writes your output to the console window opened by your application (think black window with white text that appears when you open the Command Prompt.) Try System. Diagnostics.


2 Answers

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:\test>WpfConsoleTest.exe  C:\test>Start Stop  ^^^^ The cursor will stay here waiting for the user to press enter! 

The solution is, once again, to use start:

C:\test>start /wait WpfConsoleTest.exe Start Stop 

Thanks for input!

like image 133
gwiazdorrr Avatar answered Sep 22 '22 03:09

gwiazdorrr


What I've done in the past is to make it a console application and call P/Invoke to hide the app's associated Console window once I've shown my WPF Window:

//added using statements per comments... using System.Diagnostics; using System.Runtime.InteropServices;      internal sealed class Win32     {         [DllImport("user32.dll")]         static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);          public static void HideConsoleWindow()         {             IntPtr hWnd = Process.GetCurrentProcess().MainWindowHandle;              if (hWnd != IntPtr.Zero)             {                 ShowWindow(hWnd, 0); // 0 = SW_HIDE             }         }     } 
like image 27
Steve Danner Avatar answered Sep 19 '22 03:09

Steve Danner