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 8468243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:52:38+00:00 2026-06-10T15:52:38+00:00

I want to implement the command line run in a WPF project. The entry

  • 0

I want to implement the command line run in a WPF project. The entry points has been changed in the App.cs. But the console will only blink and then disappear. How to fix that? Thanks

public class App : System.Windows.Application
{
    [STAThread]
    static void Main(string[] args)
    {
        if (args.Length > 0)
        {// Commandline run mode
            // Command line given, console from console
            Console.WriteLine("In cmd line mode");
            AllocConsole();
            CmdLineRun.ParseArgs(args);
            CmdLineRun.CmdRun();
        }
        else
        {// GUI mode
            GUIMain();
        }
    }

    static void GUIMain()
    {
        App app = new App();
        app.StartupUri = new System.Uri("MainWindow.xaml", System.UriKind.Relative);
        app.Run();
    }

    [System.Runtime.InteropServices.DllImport("kernel32.dll")]
    private static extern bool AllocConsole();
}
  • 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-10T15:52:40+00:00Added an answer on June 10, 2026 at 3:52 pm

    When you create a new console window with AllocConsole it only sticks around while your process is still active. To prevent this, you have a couple of options:

    1. Don’t actually terminate your process until the user provides some input. This is your cliche “press any key to continue” option, which you’d do after your CmdRun method returned. The console window will still disappear but only when you tell it.

    2. Use a pre-existing console. If you always run your application from a command line, you don’t need to make a new console window. Instead, use the AttachConsole method to attach your stdin/stdout to the one that already exists.

    The best bet is usually to try to AttachConsole(-1) (-1 means ATTACH_PARENT_PROCESS) first. If that returns 0, then your parent process did not have a console, or else you were unable to attach to it. Then you can fall back on AllocConsole, and deal with keeping it around after shutting down.

    This is not specific to WPF, it’s a problem any time you try to mix console and GUI code in the same application. If your app was compiled for the CUI subsystem, by specifying “console application”, Windows would handle this for you, but you wouldn’t get a message queue by default and windowed UI would fail. If your app was compiled for the GUI subsystem, by specifying “windows application”, you get a message queue but not console handling. Letting Windows handle the GUI part and managing the console by hand is by far the easier of the two options.

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

Sidebar

Related Questions

I want to implement the command line features like in a linux terminal. I
I am trying to implement a Command Line Interface for an existing project. The
I am trying to implement a command line emulator in JavaScript. I want the
i am trying to implement the more command. I want to learn that how
I want implement in my software solution an VBA editor but in c# 3.0.
Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it
I want to write a command line utility that can be used to synchronize
This is the command line I want to use inside my VB.NET program. Look
I'm trying to implement a program in Xcode that's somewhat like a command line.
I'm trying to implement a command-line interface inside of a Windows Forms form. I've

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.