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

  • Home
  • SEARCH
  • 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 6853205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:29:03+00:00 2026-05-27T01:29:03+00:00

I have a WinForm project that I want to be usable as a console

  • 0

I have a WinForm project that I want to be usable as a console app if certain arguments are passed into it. Using some tips I read from here, I used the following code to make this work.

[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern bool AllocConsole();

[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern bool AttachConsole(int pid);

...

if (!AttachConsole(-1))
{
   AllocConsole();
}

This works, but there is one annoying side effect – all the output seems to be generated from a background thread. When I run the program from a command prompt, the output displays the next prompt before displaying the output.

I set the project properties’ Output Type to Console Application and it fixed this problem, but now there is always a console window displayed – even when it is run in ‘WinForm mode’.

Is there a way to get the best of both worlds where the console behaves like the program is the current process and the console window is not displayed when the program displays the WinForm?

UPDATE:
My apologies for not clarifying this. I am toggling between console and WinForm mode in Program.cs like this:

// nowin is a bool that is set based on a parameter
if (nowin)
{
    if (!AttachConsole(-1))
    {
        AllocConsole();
    }
    //... Console mode code here...
}
else
{
    // run in window
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1(argDict));
}
  • 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-05-27T01:29:04+00:00Added an answer on May 27, 2026 at 1:29 am

    It looks like there is no way around it:
    1) If I want the output in console mode to behave like a dedicated console app, the console window must always be there.
    2) If I want to hide the console when I run it in windowed mode, I will lose the blocking prints.

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

Sidebar

Related Questions

I have a college project that I want to do as 3 Tier Winform
I have a WinForm project that contains a form called MainUI. You can see
I have a windows installer project which installs some software (winform, service, mce addin).
I am using C# and developing a winform application. I have a project class
I want to create a Visual Studio 2010 setup project that deploys some files
I have a winforms project that I want to develop a custom control for.
I am doing college project in winform, mysql ,c#. for that I have created
I have a class library that I created using the Class Library project template.
I have three c# projects in my solution. One is a console app that
I have a VERY simple C# Winforms project that I made using Visual Studio

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.