Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8253627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:56:28+00:00 2026-06-08T00:56:28+00:00

Background: I am struggling to add command line and batch processing capabilities to an

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:56:30+00:00Added an answer on June 8, 2026 at 12:56 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm struggling to add a vertical line to a custom Android Notification. I've tried
I've been struggling for a while now trying to add a good looking background
I've been struggling for some time with adding In App Purchase to an existing
Coming from a non-web background I'm struggling with cookie uniqueness. When I read and
I am struggling to set an background image for an QPushButton. No Success till
Background: We're building an application that allows our customers to supply data in a
Background : I'm trying to convert some JavaScript code which uses the the Crossfilter
I'm fairly new to nHibernate having come from an EF background and I'm struggling
I am struggling with some minor layout changes in vaadin which has to be
Some background. I'm helping a group of students at my son's school and they

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.